简体   繁体   English

在Storyboard中创建的TabbarController无法以编程方式更改视图

[英]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. 我可以通过单击模拟器上的item1item2图标来更改视图,但是不能以编程方式更改视图。

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.

相关问题 如何从不在情节提要中但以编程方式创建的视图控制器呈现位于“主”情节提要中的视图控制器? - How to present a view controller that is in the “main” storyboard from a view controller that isn't in the storyboard but is created programmatically? 添加故事板视图作为以编程方式创建的视图的子视图 - Add a storyboard view as a subview of a programmatically created view 无法在Storyboard中更改视图的大小 - Can't change size of view in Storyboard 以编程方式选择TabBarController视图? - Programmatically Selecting a TabBarController View? 无法使用TabBarController在AppDelegate中推送视图控制器 - Can't push view controller in AppDelegate with TabBarController 无法在以编程方式创建的自定义视图中更改UILabel文本值 - can't change a UILabel text value inside a programmatically created custom view tabBarController是零; 无法更改viewDidLoad上的selectedIndex - tabBarController is nil; can't change selectedIndex on viewDidLoad 在Storyboard中创建的容器视图不会以编程方式更改框架 - Container View made in Storyboard will not change frame programmatically 引用从AppDelegate中的StoryBoard创建的TabbarController? - Referencing TabbarController created from StoryBoard in AppDelegate? 从我的故事板上的另一个视图中获取tabbarcontroller - Get tabbarcontroller from another view on my storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM