Created
July 31, 2010 02:22
-
-
Save marshluca/501677 to your computer and use it in GitHub Desktop.
Post like the new tweet view in twitter for iPhone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PostViewController.h" | |
#import "WoyiAppDelegate.h" | |
#import "ImagePickerController.h" | |
@implementation PostViewController | |
#pragma mark - | |
#pragma mark Initialization | |
- (id)initWithTitle:(NSString *)title | |
{ | |
if (self = [super initWithStyle:UITableViewStylePlain]) | |
{ | |
self.navigationItem.title = title; | |
self.tableView.scrollEnabled = NO; | |
appDelegate = [(id)[UIApplication sharedApplication] delegate]; | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self loadLeftBarButton]; | |
[self loadCountLabelAndCloseButton]; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
[titleView becomeFirstResponder]; | |
} | |
- (void)loadLeftBarButton { | |
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取消" | |
style:UIBarButtonItemStyleBordered | |
target:self | |
action:@selector(dismissModalController)] autorelease]; | |
} | |
- (void)loadRightBarButton | |
{ | |
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"发送" | |
style:UIBarButtonItemStyleBordered | |
target:self | |
action:@selector(commitPostAction)] autorelease]; | |
} | |
- (void)dismissModalController | |
{ | |
[self dismissModalViewControllerAnimated:YES]; | |
} | |
- (void)loadCountLabelAndCloseButton | |
{ | |
countLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 193, 50, 30)]; | |
countLabel.text = @"140"; | |
countLabel.textColor = [UIColor grayColor]; | |
countLabel.font = [UIFont boldSystemFontOfSize:15]; | |
countLabel.backgroundColor = [UIColor clearColor]; | |
[self.view addSubview:countLabel]; | |
keyboardCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(285, 195, 50, 30)]; | |
[keyboardCloseButton setTag:1]; | |
[keyboardCloseButton setImage:[UIImage imageNamed:@"image128.png"] forState:UIControlStateNormal]; | |
[keyboardCloseButton addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside]; | |
[keyboardCloseButton setHidden:YES]; | |
[self.view addSubview:keyboardCloseButton]; | |
dateCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(285, 195, 50, 30)]; | |
[dateCloseButton setTag:1]; | |
[dateCloseButton setImage:[UIImage imageNamed:@"image128.png"] forState:UIControlStateNormal]; | |
[dateCloseButton addTarget:self action:@selector(hideDatePickerView) forControlEvents:UIControlEventTouchUpInside]; | |
[dateCloseButton setHidden:YES]; | |
[self.view addSubview:dateCloseButton]; | |
cateCloseButton= [[UIButton alloc] initWithFrame:CGRectMake(285, 195, 50, 30)]; | |
[cateCloseButton setTag:1]; | |
[cateCloseButton setImage:[UIImage imageNamed:@"image128.png"] forState:UIControlStateNormal]; | |
[cateCloseButton addTarget:self action:@selector(hideCategoryPickerView) forControlEvents:UIControlEventTouchUpInside]; | |
[cateCloseButton setHidden:YES]; | |
[self.view addSubview:cateCloseButton]; | |
} | |
- (void)showAlertMessage:(NSString *)message | |
{ | |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self | |
cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alertView show]; | |
[alertView release]; | |
} | |
- (void)commitPostAction | |
{ | |
NSString *apiString = [NSString stringWithFormat:@"%@/post_mobile_event",API_SERVER]; | |
if ([despView.text length] > 0) | |
{ | |
rssParser = [[BlogRssParser alloc] init]; | |
rssParser.delegate = self; | |
rssParser.currentApiIndex = kPostMobileEvent; | |
NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc] init]; | |
[paramsDict setValue:titleView.text forKey:@"title"]; | |
[paramsDict setValue:despView.text forKey:@"description"]; | |
[paramsDict setValue:selectedCate forKey:@"category"]; | |
[paramsDict setValue:selectedDate forKey:@"end_time"]; | |
[paramsDict setValue:[NSString stringWithFormat:@"%f",appDelegate.latitude] forKey:@"lat"]; | |
[paramsDict setValue:[NSString stringWithFormat:@"%f",appDelegate.longitude] forKey:@"lng"]; | |
[paramsDict setValue:@"状态" forKey:@"sign"]; | |
[rssParser parseXMLDataWithAPI:apiString AndParams:paramsDict]; | |
[paramsDict release]; | |
} | |
else | |
[self showAlertMessage:@"标题不能为空."]; | |
} | |
- (void)hideDate | |
{ | |
selectedDate = [NSString stringWithFormat:@"%@",datePickerView.date]; | |
[datePickButton setTitle:selectedDate forState:UIControlStateNormal]; | |
[datePickerView removeFromSuperview]; | |
} | |
- (void)hideCategory | |
{ | |
[catePickerView removeFromSuperview]; | |
} | |
- (void)hideDatePickerView | |
{ | |
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; | |
CGRect endFrame = datePickerView.frame; | |
endFrame.origin.y = screenRect.origin.y + screenRect.size.height; | |
// start the slide down animation | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:0.3]; | |
// we need to perform some post operations after the animation is complete | |
[UIView setAnimationDelegate:self]; | |
[UIView setAnimationDidStopSelector:@selector(hideDate)]; | |
datePickerView.frame = endFrame; | |
[UIView commitAnimations]; | |
// grow the table back again in vertical size to make room for the date picker | |
CGRect newFrame = self.tableView.frame; | |
newFrame.size.height += datePickerView.frame.size.height; | |
self.tableView.frame = newFrame; | |
// remove the "Done" button in the nav bar | |
dateCloseButton.hidden = YES; | |
} | |
- (void)hideCategoryPickerView | |
{ | |
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; | |
CGRect endFrame = catePickerView.frame; | |
endFrame.origin.y = screenRect.origin.y + screenRect.size.height; | |
// start the slide down animation | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:0.3]; | |
// we need to perform some post operations after the animation is complete | |
[UIView setAnimationDelegate:self]; | |
[UIView setAnimationDidStopSelector:@selector(hideCategory)]; | |
catePickerView.frame = endFrame; | |
[UIView commitAnimations]; | |
// grow the table back again in vertical size to make room for the date picker | |
CGRect newFrame = self.tableView.frame; | |
newFrame.size.height += catePickerView.frame.size.height; | |
self.tableView.frame = newFrame; | |
// remove the "Done" button in the nav bar | |
cateCloseButton.hidden = YES; | |
} | |
- (void)pickImage { | |
[self presentModalViewController:[[ImagePickerController alloc] initWithViewController:self] animated:YES]; | |
} | |
- (void)pickTime { | |
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; | |
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; | |
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectZero]; | |
datePickerView.date = [NSDate date]; | |
[self showDatePickerView]; | |
} | |
- (void)pickCategory { | |
cateArray = [[NSArray alloc] initWithObjects:@"户外旅游",@"生活聚会",@"公益活动",@"娱乐演出",@"夜生活",@"体育活动",@"展览会议",nil]; | |
catePickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; | |
catePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; | |
catePickerView.dataSource = self; | |
catePickerView.delegate = self; | |
catePickerView.showsSelectionIndicator = YES; | |
[self showCategoryPickerView]; | |
} | |
- (void)showDatePickerView { | |
// check if our date picker is already on screen | |
if (datePickerView.superview == nil) | |
{ | |
[self.view.window addSubview: datePickerView]; | |
// compute the start frame | |
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; | |
CGSize pickerSize = [datePickerView sizeThatFits:CGSizeZero]; | |
CGRect startRect = CGRectMake(0.0,screenRect.origin.y + screenRect.size.height,pickerSize.width, pickerSize.height); | |
datePickerView.frame = startRect; | |
// compute the end frame | |
CGRect pickerRect=CGRectMake(0.0,screenRect.origin.y+screenRect.size.height-pickerSize.height,pickerSize.width,pickerSize.height); | |
// start the slide up animation | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:0.3]; | |
// we need to perform some post operations after the animation is complete | |
[UIView setAnimationDelegate:self]; | |
datePickerView.frame = pickerRect; | |
// shrink the table vertical size to make room for the date picker | |
CGRect newFrame = self.tableView.frame; | |
newFrame.size.height -= datePickerView.frame.size.height; | |
self.tableView.frame = newFrame; | |
[UIView commitAnimations]; | |
dateCloseButton.hidden = NO; | |
} | |
} | |
- (void)showCategoryPickerView { | |
// check if our date picker is already on screen | |
if (catePickerView.superview == nil) | |
{ | |
[self.view.window addSubview: catePickerView]; | |
// compute the start frame | |
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; | |
CGSize pickerSize = [catePickerView sizeThatFits:CGSizeZero]; | |
CGRect startRect = CGRectMake(0.0,screenRect.origin.y + screenRect.size.height,pickerSize.width, pickerSize.height); | |
catePickerView.frame = startRect; | |
// compute the end frame | |
CGRect pickerRect=CGRectMake(0.0,screenRect.origin.y+screenRect.size.height-pickerSize.height,pickerSize.width,pickerSize.height); | |
// start the slide up animation | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationDuration:0.3]; | |
// we need to perform some post operations after the animation is complete | |
[UIView setAnimationDelegate:self]; | |
catePickerView.frame = pickerRect; | |
// shrink the table vertical size to make room for the date picker | |
CGRect newFrame = self.tableView.frame; | |
newFrame.size.height -= catePickerView.frame.size.height; | |
self.tableView.frame = newFrame; | |
[UIView commitAnimations]; | |
cateCloseButton.hidden = NO; | |
} | |
} | |
- (void)showGeoView { | |
UILabel *geoLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 198, 250, 20)]; | |
geoLabel.text = [NSString stringWithFormat:@"%f,%f %@",appDelegate.longitude,appDelegate.latitude,appDelegate.currentAddress]; | |
geoLabel.textColor = [UIColor grayColor]; | |
geoLabel.font = [UIFont systemFontOfSize:12]; | |
[self.view addSubview:geoLabel]; | |
} | |
- (void)closeKeyboard | |
{ | |
NSInteger tag = [keyboardCloseButton tag]; | |
if (tag == 0) | |
{ | |
[despView becomeFirstResponder]; | |
keyboardCloseButton.hidden = NO; | |
} | |
else if (tag == 1) | |
{ | |
[despView resignFirstResponder]; | |
keyboardCloseButton.hidden = YES; | |
} | |
else if (tag == 2) | |
{ | |
[titleView resignFirstResponder]; | |
keyboardCloseButton.hidden = YES; | |
} | |
else | |
{ | |
[titleView becomeFirstResponder]; | |
keyboardCloseButton.hidden = NO; | |
} | |
} | |
#pragma mark - | |
#pragma mark BlogRssParserDelegate Methods | |
- (void)processCompleted | |
{ | |
if ([rssParser.rssItems count] > 0 && [[rssParser.rssItems objectAtIndex:0] description] > 0) | |
{ | |
[rssParser showAlertMessage:@"提交成功."]; | |
[self dismissModalViewControllerAnimated:YES]; | |
} | |
else | |
[rssParser showAlertMessage:@"提交失败."]; | |
} | |
-(void)processHasErrors | |
{ | |
[rssParser showAlertMessage:@"Please check if you are connected to internet."]; | |
} | |
#pragma mark - | |
#pragma mark Picker Data Source Methods | |
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView | |
{ | |
return 1; | |
} | |
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component | |
{ | |
return [cateArray count]; | |
} | |
#pragma mark Picker Delegate Methods | |
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component | |
{ | |
return [cateArray objectAtIndex:row]; | |
} | |
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component | |
{ | |
[pickerView selectRow:row inComponent:0 animated:YES]; | |
selectedCate = [cateArray objectAtIndex:row]; | |
[catePickButton setTitle:selectedCate forState:UIControlStateNormal]; | |
} | |
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component | |
{ | |
return 150; | |
} | |
#pragma mark - | |
#pragma mark UITexFieldDelegate Methods | |
- (void)textFieldDidBeginEditing:(UITextField *)textField | |
{ | |
[keyboardCloseButton setTag:2]; | |
keyboardCloseButton.hidden = NO; | |
} | |
- (void)textFieldDidEndEditing:(UITextField *)textField | |
{ | |
[keyboardCloseButton setTag:3]; | |
} | |
#pragma mark - | |
#pragma mark UITexViewDelegate Methods | |
- (void)textViewDidBeginEditing:(UITextView *)textView { | |
[keyboardCloseButton setTag:1]; | |
keyboardCloseButton.hidden = NO; | |
} | |
- (void)textViewDiddEndEditing:(UITextView *)textView { | |
[keyboardCloseButton setTag:0]; | |
} | |
- (void)textViewDidChange:(UITextView *)textView | |
{ | |
if ([textView.text length] > 0) | |
[self loadRightBarButton]; | |
if ([textView.text length] == 0) | |
self.navigationItem.rightBarButtonItem = nil; | |
countLabel.text = [NSString stringWithFormat:@"%d",140-[textView.text length]]; | |
} | |
#pragma mark - | |
#pragma mark Table view Helper Methods | |
- (void)loadTitleViewForCell:(UITableViewCell *)cell | |
{ | |
titleView = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 310, 30)]; | |
titleView.delegate = self; | |
titleView.placeholder = @"主题:"; | |
titleView.font = [UIFont boldSystemFontOfSize:14]; | |
[cell.contentView addSubview:titleView]; | |
} | |
- (void)loadDespViewForCell:(UITableViewCell *)cell | |
{ | |
despView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)]; | |
despView.delegate = self; | |
despView.font = [UIFont systemFontOfSize:14]; | |
[cell.contentView addSubview:despView]; | |
} | |
- (void)loadOtherViewForCell:(UITableViewCell *)cell | |
{ | |
photoPickButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 160, 110)]; | |
[photoPickButton setTitle:@"图片" forState:UIControlStateNormal]; | |
photoPickButton.backgroundColor = [UIColor grayColor]; | |
[photoPickButton addTarget:self action:@selector(pickImage) forControlEvents:UIControlEventTouchUpInside]; | |
[cell.contentView addSubview:photoPickButton]; | |
geoPickButton = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 160, 110)]; | |
[geoPickButton setTitle:@"经纬" forState:UIControlStateNormal]; | |
geoPickButton.backgroundColor = [UIColor darkGrayColor]; | |
[geoPickButton addTarget:self action:@selector(showGeoView) forControlEvents:UIControlEventTouchUpInside]; | |
[cell.contentView addSubview:geoPickButton]; | |
datePickButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 110, 160, 110)]; | |
[datePickButton setTitle:@"时间" forState:UIControlStateNormal]; | |
datePickButton.backgroundColor = [UIColor darkGrayColor]; | |
datePickButton.titleLabel.numberOfLines = 2; | |
[datePickButton addTarget:self action:@selector(pickTime) forControlEvents:UIControlEventTouchUpInside]; | |
[cell.contentView addSubview:datePickButton]; | |
catePickButton = [[UIButton alloc] initWithFrame:CGRectMake(160, 110, 160, 110)]; | |
[catePickButton setTitle:@"分类" forState:UIControlStateNormal]; | |
catePickButton.backgroundColor = [UIColor grayColor]; | |
[catePickButton addTarget:self action:@selector(pickCategory) forControlEvents:UIControlEventTouchUpInside]; | |
[cell.contentView addSubview:catePickButton]; | |
} | |
#pragma mark - | |
#pragma mark Table view data source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return 3; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; | |
cell.accessoryType = UITableViewCellAccessoryNone; | |
cell.selectionStyle = UITableViewCellSelectionStyleNone; | |
} | |
if (indexPath.row == 0) | |
[self loadTitleViewForCell:cell]; | |
else if (indexPath.row == 1) | |
[self loadDespViewForCell:cell]; | |
else | |
[self loadOtherViewForCell:cell]; | |
return cell; | |
} | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (indexPath.row == 0) | |
return 40.0; | |
else if(indexPath.row == 1) | |
return 180.0; | |
else | |
return 260.0; | |
} | |
#pragma mark - | |
#pragma mark Memory management | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Relinquish ownership any cached data, images, etc that aren't in use. | |
} | |
- (void)viewDidUnload { | |
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. | |
// For example: self.myOutlet = nil; | |
} | |
- (void)dealloc | |
{ | |
[catePickButton release]; | |
[photoPickButton release]; | |
[datePickButton release]; | |
[geoPickButton release]; | |
[countLabel release]; | |
[keyboardCloseButton release]; | |
[cateCloseButton release]; | |
[dateCloseButton release]; | |
[titleView release]; | |
[despView release]; | |
[cateArray release]; | |
[appDelegate release]; | |
[rssParser release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment