从 SplitViewController 设置数组 .viewControllers

发布于 2024-12-18 08:37:57 字数 4140 浏览 2 评论 0原文

我正在尝试在 TabBar 内使用 splitview。现在我的第一个 TabBarItem 中有一个 SplitView。当我尝试访问 SplitViewController 中的不同 DetailView 或右视图时,我的问题就出现了。

我试图在 SplitView 的根(或主)视图控制器的 didSelectRowAtIndexPath: 内部执行此操作。

这是代码,我尝试从 AppDelegate 对象访问 TabBarController,并更改 SplitView 的 viewControllers 数组,仅更改第二个视图控制器。我总是收到此崩溃错误,说第二个实例发送无法识别: -[SecondViewController viewControllers]: 无法识别的选择器发送到实例 0x6852460

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//object AppDelegate
AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//Objecte in Index 0 is my SplitVC
NSArray *barControllers = myDelegate.tabBarController.viewControllers;

if (indexPath.row == 0) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    FirstViewController *detail = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];

    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:temporalSplit.viewControllers, [barControllers objectAtIndex:1], nil];

}
else if (indexPath.row == 1) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    Detail2VC *detail = [[Detail2VC alloc]initWithNibName:@"Detail2VC" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];


    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:[barControllers objectAtIndex:0], temporalSplit, nil];
}
[myDelegate release];
}

我的 AppDelegate 代码(可以正常工作):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];

// Override point for customization after application launch.

NSMutableArray *controllersBar = [[NSMutableArray alloc]init];

UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

for (int i = 0; i<2; i++) 
{
    if(i == 0)
    {
        _firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

        _masterApp = [[MasterVC alloc]initWithNibName:@"MasterVC" bundle:nil];

        _masterApp.firstViewController = _firstViewController;
        _firstViewController.mastervc = _masterApp;

        UINavigationController *navigationMaster = [[UINavigationController alloc]initWithRootViewController:_masterApp];
        UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:_firstViewController];

        _splitVC = [[SplitVC alloc] initWithNibName:nil bundle:nil];
        //_splitVC.tabBarItem = controller.tabBarItem;
        _splitVC.viewControllers = [NSArray arrayWithObjects:navigationMaster, navigationDetail, nil];
        _splitVC.delegate = _firstViewController;

        [controllersBar addObject:_splitVC];
    }
    else if (i == 1)
    {
        [controllersBar addObject:viewController2];
    }
}

//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

_tabBarController.viewControllers = controllersBar;
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;

self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

return YES;
}

我做错了什么?欢迎任何帮助或建议。

提前感谢大家。

I'm trying to use a splitview inside a TabBar. By now I have a SplitView in my first TabBarItem. My problem comes when I try to access to a different DetailView or right view in the SplitViewController I have.

I'm trying to do it inside the didSelectRowAtIndexPath: of my Root (or Master) viewcontroller from the SplitView.

Here's the code, where I try to acces to my TabBarController from an AppDelegate object, and change the viewControllers array of my SplitView only changing the second view controller. I always get this crash error, saying that 2nd instance send is unrecognized: -[SecondViewController viewControllers]: unrecognized selector sent to instance 0x6852460

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//object AppDelegate
AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//Objecte in Index 0 is my SplitVC
NSArray *barControllers = myDelegate.tabBarController.viewControllers;

if (indexPath.row == 0) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    FirstViewController *detail = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];

    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:temporalSplit.viewControllers, [barControllers objectAtIndex:1], nil];

}
else if (indexPath.row == 1) 
{
    SplitVC *temporalSplit = [barControllers objectAtIndex:indexPath.row];

    NSArray *mArray = temporalSplit.viewControllers;

    Detail2VC *detail = [[Detail2VC alloc]initWithNibName:@"Detail2VC" bundle:nil];

    UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:detail];

    temporalSplit.delegate = detail;

    temporalSplit.viewControllers = [[NSArray alloc] initWithObjects:[mArray objectAtIndex:0], navigationDetail, nil];


    myDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:[barControllers objectAtIndex:0], temporalSplit, nil];
}
[myDelegate release];
}

And my AppDelegate code (that works without problems):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];

// Override point for customization after application launch.

NSMutableArray *controllersBar = [[NSMutableArray alloc]init];

UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

for (int i = 0; i<2; i++) 
{
    if(i == 0)
    {
        _firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

        _masterApp = [[MasterVC alloc]initWithNibName:@"MasterVC" bundle:nil];

        _masterApp.firstViewController = _firstViewController;
        _firstViewController.mastervc = _masterApp;

        UINavigationController *navigationMaster = [[UINavigationController alloc]initWithRootViewController:_masterApp];
        UINavigationController *navigationDetail = [[UINavigationController alloc]initWithRootViewController:_firstViewController];

        _splitVC = [[SplitVC alloc] initWithNibName:nil bundle:nil];
        //_splitVC.tabBarItem = controller.tabBarItem;
        _splitVC.viewControllers = [NSArray arrayWithObjects:navigationMaster, navigationDetail, nil];
        _splitVC.delegate = _firstViewController;

        [controllersBar addObject:_splitVC];
    }
    else if (i == 1)
    {
        [controllersBar addObject:viewController2];
    }
}

//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];

_tabBarController.viewControllers = controllersBar;
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;

self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

return YES;
}

What am I doing wrong? Any help or suggestion is welcome.

Thanks to all in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

☆獨立☆ 2024-12-25 08:37:57

分割视图控制器必须是根级控制器:

UISplitviewcontroller 不作为根视图控制器

如果不,你显然可以让各种奇怪的事情发生。

Split view controller MUST be root level controllers:

UISplitviewcontroller not as a rootview controller

If not, you can apparently get all sorts of strange things happening.

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