无法在 Delegate 方法中设置 ViewController 的标题
我已经实现了一个 optionTable,其中存储了我的应用程序要执行的选项列表
我在选项表中定义了一个协议方法,以便主表知道用户何时选择了一个选项
//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
在用户选择一个选项后会触发此委托方法在选项表视图中
//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_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];
}
在主表中(调用选项表)我将选项表委托设置为 self 并且还包含委托方法
//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];
}
我尝试在主表的委托方法中设置页面标题
//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];
}
我的页面标题没有在上面的委托方法中进行更改。关于在选项表中选择选项后如何更改标题有什么建议吗?
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_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试选择以下两个之一:
1)将
[self.optionTableDelegate didSelectOption:self.option withtitleString:self.titleString];
替换为[self didSelectOption:self.option withtitleString:self.titleString] ;
2) 将
self.optionsTableView.optionTableDelegate = self;
替换为self.optionTableDelegate = self;
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;
withself.optionTableDelegate = self;
您可以使用
[self.navigationItem setTitle:@"您的标题"];
更新导航栏的标题。
You can use
[self.navigationItem setTitle:@"Your Title"];
to update the title of your navBar.