无法在 Delegate 方法中设置 ViewController 的标题

发布于 2024-12-08 08:03:39 字数 2328 浏览 0 评论 0原文

我已经实现了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半寸时光 2024-12-15 08:03:39

尝试选择以下两个之一:

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; with self.optionTableDelegate = self;

孤千羽 2024-12-15 08:03:39

您可以使用 [self.navigationItem setTitle:@"您的标题"];
更新导航栏的标题。

You can use [self.navigationItem setTitle:@"Your Title"];
to update the title of your navBar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文