调用实例方法的问题

时间:2011-08-31 11:51:01

标签: iphone objective-c cocoa-touch uitableview

按照代码更改选项卡名称,然后选择索引2.

[(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"];

self.rootController.selectedIndex = 2;

然而,它适用于例如app didFinishLaunching方法但在调用时无法正常工作。

触摸内部按钮触发renameTabs:

- (IBAction) renameTabs: (id)sender
{
    CompanyAppDelegate *theInstance = [[CompanyAppDelegate alloc] init];
    [theInstance rename];

}

并在控制器中:

- (void) rename
    {

        [(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"];

        self.rootController.selectedIndex = 2;

    }

重命名函数被触发并在.h中定义。没有错误但没有任何改变!有什么不对的吗??谢谢

3 个答案:

答案 0 :(得分:3)

您不应该创建新的CompanyAppDelegate。尝试在sharedAppDelegate中实施方法CompanyAppDelegate.m

+ (CompanyAppDelegate *)sharedAppDelegate
{
    return (CompanyAppDelegate *) [UIApplication sharedApplication].delegate;
}

不要忘记在CompanyAppDelegate.h中声明它。

用这个替换renameTabs

- (IBAction) renameTabs: (id)sender
{
    CompanyAppDelegate *theInstance = [CompanyAppDelegate sharedAppDelegate];
    [theInstance rename];
}

答案 1 :(得分:1)

- (IBAction) renameTabs: (id)sender
{
    CompanyAppDelegate *theInstance = (CompanyAppDelegate *) [UIApplication sharedApplication].delegate;
    [theInstance rename];

}

- (void) rename
    {

        [(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"];

        self.rootController.selectedIndex = 2;

    }

答案 2 :(得分:0)

<啊,啊,当我第一次看到这个时,我以为你说重命名没有到达。没关系。]