Musterknabe
Musterknabe

Reputation: 6081

Switch between different Views with a Navigation Controller

I'm totally new to iOS programming. I only programmed on Android so far and Objective-C is a total different and new language for me now.

What I want to do is to not use a design that I've created with the storyboard. I want to do all programmatically, since I think it will be more dynamic if I do it like this.

The problem I'm encountering is, that I want to have 3 different views. I googled a bit, and stumbled upon some stackoverflow questions. There, people suggested using a NavigationController. Okay. Now I'm trying to implement it. What I want to have is the following

A MainViewController that has 3 different views. The first view is a loginView. The second one is displaying data and the third is displaying detailed data dependent on the click of the second view.

Is a navigationcontroller corerct for this? The problem I'm having is where I tell the app that I want to start with the MainViewController and push the LoginView in it.

I have a MainViewController.h and MainViewController.m that are subclasses of UIViewController

Now, where exactly do I do this? I have the didFinishLaunchingWithOptions method right here with the following content

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window                                     = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor                     = [UIColor whiteColor];

    UIViewController *viewController                = [[MainViewController alloc]init];
    UINavigationController *navigationController    = [[UINavigationController alloc] initWithRootViewController:viewController];

    [navigationController pushViewController:viewController animated:NO];
    self.window.rootViewController                  = navigationController;
    [self.window makeKeyAndVisible];
    return YES;

}

But that just crashes the app. What am I doing wrong? How do I get those three views? Am I starting completely wrong? Please help. As I said I'm new to iOS development. It's easy for me to programm on one view. I did that already, but I want thre different views! Thanks!

Upvotes: 2

Views: 3615

Answers (4)

dheerendra
dheerendra

Reputation: 537

use this code and if application is universion then use same code else remove the condition of ([[UIDevice currentDevice] userInterfaceIdiom]

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navController;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

      self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];


  {
                          if(result.length>0)
                          {
                              for(var i in result)
                              {
                                var ObjResult=result[i];

                                      var content = "<div data-role='collapsible' id='set" + i + "'>";
                                      content+="<h3>"+ObjResult.title+"<br>";

                                      var intDate=parseInt(ObjResult['ordered date']);
                                      content +=timestampToDate(intDate)+"</h3>"

                                      if(isNaN(ObjResult.med_placeorderfor))
                                      content+="<p><a>Medicle Place order for: </a>"+result[i].med_placeorderfor+"</p>";

                                      if(isNaN(ObjResult.pres_placeorderfor)>0)
                                      content+="<p><a>Medicle Place order for: </a>"+result[i].placeorderfor+"</p>";

                                      if(ObjResult['order status'].length>0)
                                      content+="<p><a>Order status: </a>"+ObjResult['order status']+"</p>";

                                      if(ObjResult.comments.length>0)
                                      content+="<p><a>Comments: </a>"+ObjResult.comments+"</p>";

                                      content+="</div>";
                                }
                                  $("#id_notification_list_dashboard").append( content ).collapsibleset('refresh');
                                  $("#id_notification_list_dashboard").trigger('create');

                      }
                      else
                      {
                      $("#id_notification_list_dashboard").append("<div style=\"text-align:center\" data-role='list-divider'><h1>No data found</h1></div>").collapsibleset('refresh');
                      }
                      $('body').removeClass('ui-loading');
                      loadingWithMsg("hide");
                      }

    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }


    navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    [navController.navigationBar setTranslucent:YES];
    navController.navigationBar.tintColor = [UIColor colorWithRed:161.0f/255.0f green:18.0f/255.0f blue:6.0f/255.0f alpha:1];
    self.window.rootViewController =navController ;
    [self.window makeKeyAndVisible];
    return YES;


}

Upvotes: 1

Gonzalo Aune
Gonzalo Aune

Reputation: 131

Remove this:

[navigationController pushViewController:viewController animated:NO];

You shouldnt push the ViewController since you told the NavigationController already that the ViewController would be the root one:

UINavigationController *navigationController    = [[UINavigationController alloc]
                                                  initWithRootViewController:viewController];

Upvotes: 1

Cyrille
Cyrille

Reputation: 25144

A MainViewController that has 3 different views. The first view is a loginView. The second one is displaying data and the third is displaying detailed data dependent on the click of the second view.

That's wrong.

You need three different view controllers, each of those will manage its own view.

Then you push one after another in the navigation controller, depending on user interaction.

Upvotes: 2

Hemant Chittora
Hemant Chittora

Reputation: 3162

Yes, Gonzalo Aune is rite, You should not push the rootviewcontroller in NavicationController. Also , I will Suggest you to keep your first view (Login View) out of Navigation controller. You can start with your MainViewController and based on check and conditions you can present LoginView on MainViewController using

[self presentViewController:loginViewController animated:YES completion:NULL];

And after successful login you can dismiss LoginViewController.

Upvotes: 1

Related Questions