Problems calling instance method
Following code changes tab name and select then index 2.
[(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"];
self.rootController.selectedIndex = 2;
However, it works for example when app didFinishLaunching method but not working when called like follows.
touch up inside button triggers renameTabs:
- (IBAction) renameTabs: (id)sender
{
CompanyAppDelegate *theInstance = [[CompanyAppDelegate alloc] init];
[theInstance rename];
}
and in the controller:
- (void) rename
{
[(UITabBarItem*)[self.rootController.tabBar.items objectAtIndex:0] setTitle:@"User"];
self.rootController.selectedIndex = 2;
}
rename function is triggered and al开发者_开发技巧so defined at .h. No errors but nothing changes! Is there anything wrong?? Thank you
You shouldn't create new CompanyAppDelegate
. Try to implement method sharedAppDelegate
in CompanyAppDelegate.m
:
+ (CompanyAppDelegate *)sharedAppDelegate
{
return (CompanyAppDelegate *) [UIApplication sharedApplication].delegate;
}
Don't forget to declare it in CompanyAppDelegate.h
.
And replace renameTabs
with this one:
- (IBAction) renameTabs: (id)sender
{
CompanyAppDelegate *theInstance = [CompanyAppDelegate sharedAppDelegate];
[theInstance rename];
}
- (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;
}
[Ah, when I first read this I thought you said rename wasn't reached. Never mind.]
精彩评论