做新界面的時候看著以前的代碼然後准備整理一下一些小功能,然後開始寫一些博客: 記得實現 UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate// UserStaticProfileViewController.m // // // Created by ifziv on 13-2-25. // Copyright (c) 2013年 iChano. All rights reserved. // #import "UserStaticProfileViewController.h" #define kLogoutActionSheetTag 500 #define kPhotoActionSheetTag 501 #define kNameTag 502 #define kGardenTag 503 #define kAgeTag 504 #define kPhoneTag 505 #define kIDCardTag 506 #define kEamilTag 507 #define kAddressTag 508 @interface UserStaticProfileViewController () { UIImagePickerController *_imagepicker; UIImage *pickedImage; UIImage *_headerImage; UIImageView *avartView; NSString *avartURL; } @end @implementation UserStaticProfileViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.title = NSLocalizedString(@"alterProfile", nil); [self setLeftItem]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setLeftItem { UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:NSLocalizedString(@"back", nil) forState:UIControlStateNormal]; CGSize labelSize = [button.titleLabel.text sizeWithFont:ZY_FONT(13) constrainedToSize:CGSizeMake(160.0, 18.0)]; button.titleLabel.font = ZY_FONT(13); [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; UIImage *bg = [UIImage getStretchableImage:@"back.png" withLeftCap:30.0 withTopCap:0.0]; [button setBackgroundImage:bg forState:UIControlStateNormal]; [button addTarget:self action:@selector(leftItem:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0.0, 0.0, labelSize.width+20.0, kItemHeight); UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = leftItem; } -(void)leftItem:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } -(void)loadView { [super loadView]; self.view.backgroundColor = [UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0]; self.view.userInteractionEnabled = YES; UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 80.0)]; [imageBg setContentMode:UIViewContentModeScaleAspectFill]; [imageBg setClipsToBounds:YES]; imageBg.image = [UIImage imageNamed:@"infoProfile.jpg"]; [self.view addSubview:imageBg]; avartView = [[UIImageView alloc] initWithFrame:CGRectMake(125.0, 45.0, 70.0, 70.0)]; avartView.layer.cornerRadius = avartView.frame.size.height/2; avartView.userInteractionEnabled = YES; avartView.layer.masksToBounds = YES; [avartView setContentMode:UIViewContentModeScaleAspectFill]; [avartView setClipsToBounds:YES]; avartView.layer.shadowColor = [UIColor whiteColor].CGColor; avartView.layer.shadowOffset = CGSizeMake(4.0, 4.0); avartView.layer.shadowOpacity = 0.5; avartView.layer.shadowRadius = 2.0; avartView.layer.borderColor = [UIColor whiteColor].CGColor; avartView.layer.borderWidth = 2.0f; avartView.image = [UIImage imageNamed:@"profileIcon.png"]; if (_userProfile) { avartURL = [NSString stringWithString:_userProfile.avatar]; NSString *avatarPath = [[FileManager getClassInstance] getAvatarMessageFolder:loginUserJid withImageUrl:_userProfile.avatar]; NSData *imageData = [NSData dataWithContentsOfFile:avatarPath]; if(imageData) { UIImage *image = [UIImage imageWithData:imageData]; image = [image roundedCornerImage:10 borderSize:3]; avartView.image = image; } else avartView.image = [UIImage imageNamed:@"profileIcon.png"]; } else { avartView.image = [UIImage imageNamed:@"profileIcon.png"]; } [self.view addSubview:avartView]; UIView *separator = [[UIView alloc]init]; [separator setFrame:CGRectMake(0.0, 125.0, 320.0, 1.0)]; separator.backgroundColor = ZY_COLOR(211.0, 211.0, 211.0, 1.0); [self.view addSubview: separator]; UIButton *btnNoLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnNoLogin.frame = CGRectMake(10.0, 385.0, 300.0, 45.0); [btnNoLogin setBackgroundColor:[UIColor colorWithRed:224.0/255.0 green:125.0/255.0 blue:123.0/255.0 alpha:1.0]]; btnNoLogin.layer.cornerRadius = 4.0; btnNoLogin.titleLabel.font = ZY_FONT(17.0); [btnNoLogin setTitle:@"退出登錄" forState:UIControlStateNormal]; [btnNoLogin setTintColor:[UIColor whiteColor]]; [btnNoLogin addTarget:self action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnNoLogin]; UIButton *btnPW = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnPW.frame = CGRectMake(10, 435.0, 300.0, 45.0); [btnPW setBackgroundColor:[UIColor colorWithRed:125.0/255.0 green:189.0/255.0 blue:164.0/255.0 alpha:1.0]]; btnPW.layer.cornerRadius = 4.0; btnPW.titleLabel.font = ZY_FONT(17.0); [btnPW setTitle:@"修改密碼" forState:UIControlStateNormal]; [btnPW setTintColor:[UIColor whiteColor]]; [btnPW addTarget:self action:@selector(changePassword:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnPW]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setPhoto:)]; [avartView addGestureRecognizer:singleTap]; } #pragma mark - #pragma mark UIActionSheetDelegate && Camera - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.tag == kLogoutActionSheetTag) { if (buttonIndex == 0) { [self performSelectorOnMainThread:@selector(xmppLogoutAct) withObject:self waitUntilDone:YES]; } } else if (actionSheet.tag == kPhotoActionSheetTag) { if (buttonIndex == 0) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { [self takePhoto]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil//NSLocalizedString(@"popup_alert", nil) message:NSLocalizedString(@"system_settings_device_no_camera", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"confirm_button", nil) otherButtonTitles:nil]; [alert show]; return; } } if (buttonIndex == 1) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { [self choosePhoto]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil//NSLocalizedString(@"popup_alert", nil) message:NSLocalizedString(@"system_settings_device_no_camera", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"confirm_button", nil) otherButtonTitles:nil]; [alert show]; return; } } } } - (void) takePhoto { if (!_imagepicker) _imagepicker = [[UIImagePickerController alloc] init]; _imagepicker.delegate = self; _imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera; _imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; _imagepicker.allowsEditing = YES; [self presentModalViewController:_imagepicker animated:YES]; } - (void) choosePhoto { if (!_imagepicker) _imagepicker = [[UIImagePickerController alloc] init]; _imagepicker.delegate = self; _imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; _imagepicker.allowsEditing = YES; [self presentModalViewController:_imagepicker animated:YES]; } - (void)startUploadImage:(UIImage*)photoImage { _headerImage = photoImage; [self uploadAvartReceive]; } - (void)uploadAvartReceive { avartView.image = _headerImage; [MBProgressHUD showWithLabel:NSLocalizedString(@"success", nil) withView:self.view]; } - (void)setPhoto:(id)sender { UIActionSheet *actionSheetPhoto = [[UIActionSheet alloc]initWithTitle:NSLocalizedString(@"selectImage", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"cancle", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"takePhoto", nil), NSLocalizedString(@"photoGallery", nil) ,nil]; [actionSheetPhoto dismissWithClickedButtonIndex:0 animated:YES]; actionSheetPhoto.actionSheetStyle = UIActionSheetStyleBlackTranslucent; actionSheetPhoto.tag = kPhotoActionSheetTag; [actionSheetPhoto showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { ZYLog(@"didDismissWithButtonIndex:%d",buttonIndex); } #pragma mark - #pragma mark UIImagePickerControllerDelegate - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissModalViewControllerAnimated:YES]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; pickedImage = [info valueForKey:UIImagePickerControllerEditedImage]; CGSize imgSize = pickedImage.size ; if ( imgSize.width > imgSize.height ) imgSize.height = imgSize.width ; else imgSize.width = imgSize.height ; pickedImage = [UIImage createImageWithSize:imgSize FromImage:pickedImage]; [self performSelectorInBackground:@selector(startUploadImage:) withObject:pickedImage]; [_imagepicker dismissModalViewControllerAnimated:YES]; } - (void)logout:(id)sender { UIActionSheet *actionSheetOut = [[UIActionSheet alloc]initWithTitle:NSLocalizedString(@"logoutAlert", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"cancle", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"logoutOk", nil) ,nil]; [actionSheetOut dismissWithClickedButtonIndex:0 animated:YES]; actionSheetOut.actionSheetStyle = UIActionSheetStyleBlackTranslucent; actionSheetOut.tag = kLogoutActionSheetTag; [actionSheetOut showInView:self.view]; } #pragma mark - #pragma mark - changPassword - (void)changePassword:(id)sender { ChangePwViewController *changePwViewController = [[ChangePwViewController alloc] init]; [self.navigationController pushViewController:changePwViewController animated:YES]; } @end