Chris B
Chris B

Reputation: 55

How do you change the detailViewController in a method

I have a view that is doing a lot of functions and when I get to the point that I am done I want to change to a newViewcontroller. if I where to do this from the rootview I just call.

NewPageViewController *newDetailViewController = [[NewPageViewController alloc] initWithNibName:@"NewPageViewController" bundle:nil];
detailViewController = newDetailViewController;

But I need to do it from my old detail (the right side)

I am downloading a file in a splitview iPad app from the right side and after the file is downloaded I need to in my method change the right side of the splitview to a new nib file so I can open and edit the file

Can someone point me in the right way.

Now I have :

-(void)changeView { 

    ListController *newDetailViewController = [[ListController alloc] initWithNibName:@"ListController"bundle:nil]

    NSArray *viewControllers = [NSArray arrayWithObjects:[splitViewController.viewControllers objectAtIndex:0], newDetailViewController, nil];

    splitViewController.viewControllers = viewControllers;
    [viewControllers release];

}

-(void)downloadfile {
 //I do all my work and get the file.
NSLog(@"I need to change views now.");


                [self changeView];

}

I don't get any errors but the right side view is not changing.

Upvotes: 0

Views: 6471

Answers (3)

daris mathew
daris mathew

Reputation: 429

As @chris mentioned you can the use Delegate of UISplitViewController for iOS 8 and above which is the best possible way.

 -(void)showDetailViewController:(UIViewController *)vc sender:(nullable id)sender NS_AVAILABLE_IOS(8_0);

Upvotes: 0

chris
chris

Reputation: 16443

As of iOS8 you can use the -showDetailViewController:sender: method on the UISplitViewController. See the Apple docs on UISplitViewController.

Upvotes: 8

occulus
occulus

Reputation: 17014

There is an NSArray *viewControllers property on the UISplitViewController class. The first item in this array is your master VC, the second in the detail VC. Re-assign this property to a new array containing the same master VC but a new details VC:

// don't forget to set the delegate of myNewDetailViewController appropriately!
myNewDetailViewController.delegate = ...

NSArray newVCs = [NSArray arrayWithObjects:[uiSplitVC.viewControllers objectAtIndex:0], myNewDetailViewController, nil];

uiSplitVC.viewControllers = newVCs;

API ref for UISplitViewController: http://developer.apple.com/library/ios/#documentation/uikit/reference/UISplitViewController_class/Reference/Reference.html

N.B: do not try replacing the master VC -- it usually goes horribly wrong somehow. I tried many many ways of replacing the master, it always went wrong in some very annoying way. Replacing the detail VC is fine though!

Upvotes: 8

Related Questions