开发者

iPhone - nesting presentModalView within another Modal View

开发者 https://www.devze.com 2023-03-25 05:10 出处:网络
My question is similar to: iPhone modal view inside another modal view? and Can I push a modal view controller from within another modal view controller?, however there is one difference:

My question is similar to: iPhone modal view inside another modal view? and Can I push a modal view controller from within another modal view controller?, however there is one difference:

None of my controllers are UINavigationControllers (i think thats what they are called).

So basically I have this:

RootView (variety of options such as submit expense, check calendar, etc) --> submit expense chosen and the following code is called:

EXP = [[ExpensesViewController alloc] initWithNibName:@"ExpensesViewController" bundle:nil];

    [self presentModalViewController:EXP animated:YES];

So now there is one modalview on top of the main view. Then within the expense modal view, I'd like to be able to choose a button that opens a modal view (in similar code)...

I understand from the two questions in my intro above that to do this basically it looks like this:

[controllerA presentModalViewController:Number2 animated:YES];

Could it look like this instead:

[self.view presentModalViewController:Number2 animated:YES];

because I don't know how to reference the viewcontroller without creating a new instance of it.

Also, when it comes time to dismiss the second one so I can return to the first modal view (basically I open a modalview to submit an expense and then as part of the form, they must select a choice -- for UI purposes a new modalview is the best. Once they decide on a choice, they return to the expense view by closing the extra detail modal view, to submit the expense and once they do, then that modal view closes).

So instead of using:

[self dismissModalViewControllerAnimated:YES];

would it be:

[self.view d开发者_C百科ismissModalViewControllerAnimated:YES];

??

Thanks guys:)


Only UIViewController and subclasses thereof can present modal view controllers. self.view is a UIView instance and something completely different.

In order to save yourself allot of work you should stick to two rules:

  1. Only present new modal view controllers from a view controller like so: [self presentModal…];.
  2. Only dismiss a modal view controller from the view controller being presented, like so: [self dismiss…];.

This way you have a clear view of the ownership and responsibility of each view controller. Also the method I have seen pretty much everywhere top use [self.super dismiss…]; to dismiss a view controller will break on iOS 5.


You need to use the delegate pattern here. Make the root view controller delegate of the first, the first view controller the delegate of the second and so on.

When you want to dismiss call the delegate method and then you can dismiss the view using the self reference.

So in second view controller you will be calling [delegate dismissTheView] which will be implemented in first and will call [self dismissModalViewControllerAnimated]; and follow the same backwards till you reach your last view.

0

精彩评论

暂无评论...
验证码 换一张
取 消