We help IT Professionals succeed at work.

presentModalViewController inside viewload

BTMExpert asked
I have a view that is loaded when the app is loaded.  I want another view to go over it when that view is loaded and when someone hits a button on the second view, it goes away revealing the first view again.  

- (void)viewDidLoad
    [super viewDidLoad];
    if ([[self name] getUsername]) {
        savedname.text = [[self name] getUsername];
    } else {
        savedname.text = @"Domonique";
        LoginView *logins = [[LoginView alloc] init];
        [self presentModalViewController:logins animated:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Dom" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
	// Do any additional setup after loading the view, typically from a nib.

Open in new window

When i load that code, it spits out the alert but no presentModalViewController
Watch Question

You are not having much luck with this issue.

Here is aquick and dirty tutorial to help you understand your issues.

1. I am wondering what SELF is?  
2. I am wondering if having this in the viewDidLoad is the best?
3. I am wondering if adding a modal view and then an alert on top is a good idea?

1 - what is it
2 - can you try this elsewhere
3. - can you just take this off for the moment.
In Xcode, when you create new project, you can choose Utility Application and it will be exactly as you want. Also it is the best tutorial - you'll see the code creating new view controller and destroying it by pressing the button - exactly as you asked.

If you are looking for an alert with the user name and password, you may use AlertViewStyle (UIAlertViewStyleLoginAndPasswordInput):

Here is a blog post about it:

by the way the reason I am getting you to do a couple of things is because you code you posted looks fine to me.


1. self is the view and name is a instance of a class
2. I also tried it in the viewwillappear but i get the same result


That must be something new to UIAlertviews.  It seems cool.  That's what I want to do but inside of an alert, i want it to be another view.  Whenever I get that to work, I need the text on the first view to change depending on if you're logged in or not.
did you try it without the alert?

Yes in iOS5 the UIAlertView class got some work done on it.


Yea.  I didn't have the alert in there the first go round.  I put it in to see if that method was being called
And have you tried taking it our of viewDidLoad - ModalViewControllers can only be shown from views that are already on the screen.


So i have View A that is loaded when the app loads.  I need View B to show up if there's no info in my name class which there shouldn't be when you first come to the app.  Then after that, the view shows up and sets the value in my class and goes away showing View A again with new text on it.  I'm thinking I need to run that check on viewDidLoad method since it's ran at the beginning of the view
by the way irrespective of what kind of view you chose to present modally i guarantee you did not get it to presnt on a view which is still at viewdidload stage.  You at laest moved it to viewDidAppear.

In other words I also answered your question.


Yes you did that's why i would like to give credit where credit is due.  It's still not 100% but I have to post another question so look for that?
you know you can split points.