[英]Created TabbarController in Storyboard can't change view programmatically
Created a tabbarcontroller
as a detail view of splitview controller. 创建了一个
tabbarcontroller
作为splitview控制器的详细视图。 I can change the view by clicking item1
, item2
icons on simulator, but can not change view programmatically. 我可以通过单击模拟器上的
item1
, item2
图标来更改视图,但是不能以编程方式更改视图。
I am getting null
when try to print viewcontrollers
in nslog
. 当尝试在
nslog
打印viewcontrollers
时,我得到的是null
。 In MasterView: 在MasterView中:
@property (strong, nonatomic) TabBarRootViewController *detailViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
self.detailViewController=[[TabBarRootViewController alloc] init];
//tried also
self.detailViewController = (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//this sends object info to detail
if (indexPath.section==0) {
//send row number
NSNumber *i = [NSNumber numberWithInteger:indexPath.row];
NSLog(@"Selected index %@",i);
self.detailViewController.detailItem = i;
}
}
In detail(Tabbar): 详细信息(标签):
@property (strong, nonatomic) id detailItem;
if (self.detailItem) {
NSInteger i=[self.detailItem integerValue];
NSLog(@"recieved integer is %i",i);
//tried this
self.tabBarController.selectedIndex=i;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:i];
//list of viewcontrollers
NSArray *array;
array = [[self tabBarController] viewControllers];
NSLog(@"array %@",array);
}
NSLOG:
recieved integer is 1
array (null)
How can I change the view programmatically? 如何以编程方式更改视图?
Thanks, 谢谢,
S 小号
Looks like your tab bar controller is nil
. 看起来您的标签栏控制器为
nil
。 Maybe not correctly linked with storyboard? 也许未正确链接到情节提要?
You need to use performSegueWithIdentifier:sender:
on your current controller. 您需要在当前控制器上使用
performSegueWithIdentifier:sender:
This is because your controller is now controlled by the storyboard and it must maintain state etc. 这是因为您的控制器现在由情节提要板控制,并且必须保持状态等。
Note that you need to give your segues an ID in the storyboard editor and you cannot use your own inits, instead you have to override prepareForSegue:sender:
to inject properties. 请注意,您需要在情节提要编辑器中为Segue提供一个ID,并且不能使用自己的init,而是必须重写
prepareForSegue:sender:
来注入属性。
Good luck. 祝好运。
The problem was not able to get the exact pointer to theTabbarcontroller. 该问题无法获得指向Tabbar控制器的确切指针。
I removed the navigation controller and left only tabbar controller.Also removed the [topcontroller]
requests in master view and appdelegate. 我删除了导航控制器,只留下了
[topcontroller]
控制器。还删除了主视图和appdelegate中的[topcontroller]
请求。
So final working code is 所以最终的工作代码是
appdelegate: 代理:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
masterview: - (void)viewDidLoad masterview:-(void)viewDidLoad
{
[super viewDidLoad];
self.detailViewController = (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];
}
in tabbarcontroller (detail controller) 在tabbarcontroller中(详细信息控制器)
@property (strong,nonatomic) UITabBarController *rootController;
self.rootController= (TabBarRootViewController *)[self.splitViewController.viewControllers objectAtIndex:1];
self.rootController.selectedIndex=i;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.