Unable to set ViewController's title in Delegate method
I have implemented an optionTable with stores a list of options for my app to execute
I have defined a protocol method within the option table so that the main table knows when the user has selected an option
//Protocol defined in **OPTIONSTABLEVIEW**
@protocol OptionsTableDelegate <NSObject>
-(void)didSelectOption:(NSString *)option withtitleString:(NSString*)titleString;
@end
//Set the delegate property
@interface OptionsTableView : UITableView <UITableViewDataSource,UITableViewDelegate>
{
id optionTableDelegate;
}
@end
This delegate method is triggered after the user selects an option in the optionstableview
//User selects an option and the delegate method is triggered
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case OPTION_LATEST:
self.option = @"for_you";
self.titleString = [self.optionsArray objectAtIndex:OPTION_开发者_StackOverflow社区LATEST];
break;
case OPTION_RANDOM:
self.option = @"random";
self.titleString = [self.optionsArray objectAtIndex:OPTION_RANDOM];
break;
default:
break;
}
//Delegate method triggered to return the option and title string to the main table
[self.optionTableDelegate didSelectOption:self.option withtitleString:self.titleString];
}
In the main table (calling options table) I setup the options table delegate to self and also included the delegate method
//Main table **QUESTIONTABLEVIEWCONTROLLER**
- (void)viewDidLoad
{
[super viewDidLoad];
//Set up options tableview
self.optionsTableHeight = 24 + (44*2);
CGRect optionsTableFrame = CGRectMake(0, -optionsTableHeight, 320, optionsTableHeight);
OptionsTableView *tempOptionsTable = [[OptionsTableView alloc]initWithFrame:optionsTableFrame style:UITableViewStyleGrouped];
self.optionsTableView = tempOptionsTable;
//Setting the delegate to self
self.optionsTableView.optionTableDelegate = self;
[self.view addSubview:self.optionsTableView];
}
And I tried to set the page title in the delegate method of the main table
//Delegate method in main table
-(void)didSelectOption:(NSString *)option withtitleString:(NSString *)titleString
{
//Set title here (doesn't work)
self.title = titleString;
NSLog(@"title :%@",self.title);
self.type = option;
[self.optionsTableView slideOptionsTableOut];
[self getData];
}
The title of my page doesn't get change in the above delegate method. Any advise on how I can change the title after selecting the option in the options table?
Try to make one of two:
1) Replace [self.optionTableDelegate didSelectOption:self.option withtitleString:self.titleString];
with [self didSelectOption:self.option withtitleString:self.titleString];
2) Replace self.optionsTableView.optionTableDelegate = self;
with self.optionTableDelegate = self;
You can use [self.navigationItem setTitle:@"Your Title"];
to update the title of your navBar.
精彩评论