[Classroom Practice and Projects] Integrated Use of Navigation Controller and TabBarController and Error-prone Analysis (including message alerts, app updates)...

Links to the original text: http://www.cnblogs.com/riasky/p/3471284.html

Statement: When we use TabBarController, we always work with NavagationController. But what kind of pool is it when it's not united? Let's introduce a single TabBar Controller. Finally, review the perfect effect of the combination of the two spaces.

,, ----------------------------------------- The use of separate Tabbar Controller

1. Let's use Tabbar Controller to create the view first.


Since there is nothing in the h file, let's jump directly to the agent's implementation file.

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    LCFirstViewController *firstVC = [[LCFirstViewController alloc]init];
    LCSecondViewController *secondVC = [[LCSecondViewController alloc]init];
    

     NSArray *array = [NSArray arrayWithObjects:firstVC,secondVC, nil];
    
    UITabBarController *tabBarVC = [[UITabBarController alloc]init];
    tabBarVC.viewControllers = array;
    [self.window setRootViewController:tabBarVC];
    
    [self.window makeKeyAndVisible];
    return YES;
}

As you can see, we encapsulated two VC files into an Array group and put them under the view Controllers of UITabBarController. In this way, our two views are ready, and when we run them, we may find that our APP is blank. Well, I forgot to say that we can set some signs. All button s and everything are temp.

 

So how do we set our item name as a meaningful name?

2. Look at the init of our first ViewController and you'll see.

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

     // self.title = @"first";
        self.tabBarItem.title = @"firstView";
      self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
        

    }
    return self;
}


You may be surprised that we don't have tabbarItem in our h m file, but why does self appear after that?

 

Because it's apple because it's a wrapped framework for us, but when we don't use it, it's hidden. Sometimes, it must be set to no.

If you remove the self.title = @ "first" of my comment and the following sentence, you will find that the name of our item will change. Is that why? What is the role of self.title? We'll let you know after the introduction of navigation. Don't worry.


// Integrated Use of Navigation Controller and TabBarController

3. Now let's modify some of the code in delegate.m to achieve comprehensive use.

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    LCFirstViewController *firstVC = [[LCFirstViewController alloc]init];
    LCSecondViewController *secondVC = [[LCSecondViewController alloc]init];
    
    
    UINavigationController *navFirstVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
   UINavigationController *navSecondVC = [[UINavigationController alloc]initWithRootViewController:secondVC];
 
  NSArray *array = [NSArray arrayWithObjects:navFirstVC,navSecondVC, nil];
  
    
    UITabBarController *tabBarVC = [[UITabBarController alloc]init];
    tabBarVC.viewControllers = array;
    [self.window setRootViewController:tabBarVC];
    
    [self.window makeKeyAndVisible];
    return YES;
}


In fact, it's very simple. We just modify the attributes of vc in our array, from the original vc to the present nvc (Navigation vc). At this point, we will let a vc have the properties of the navigation bar.

 

Come on, see the effect.



4. After reading it, you may ask, what is the difference between 1.2 and 1.2?

Let's take a look at the init method of 1.2m. The comparison is good or bad.~

1.m

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

      self.title = @"first";
        self.tabBarItem.title = @"firstView";
      self.tabBarItem.image = [UIImage imageNamed:@"1.png"];
        

    }
    return self;
}


2.m

 

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tabBarItem.title = @"secondView";
        // Custom initialization
    }
    return self;
}

When you see 2, why do you think the title is set, but it is not displayed?

 

I have tried this question four times, and finally I understand.

1) Setting only one item does not appear. The above code has been verified

2) Setting the title of navigation, only the title of navigation appears.

3. Set it to self.title, all of them appear.



4) tabbarItem.title and image are set together, all of them appear.



5. If we set self.title and set self.tabbaritem.title again, we will find that the two can be different.



3. Now let me summarize a few points that I feel need to make a statement:

i: When you write code to add images, you will find that our images are blue boxes like icons, not the pictures we need, because tabbarItem needs a picture without background, and we add a picture with background.

ii: Normally, don't just use self.tabBarItem.title= @ "XXX" because we will find that this line of code does not work, either I add image, or we add self.title, so that the display we set will succeed.

iii: Tips and update reminders about prompt messages.


IV: Application update alert



 

Reprinted at: https://www.cnblogs.com/riasky/p/3471284.html

Posted by jamz310 on Wed, 24 Jul 2019 02:03:35 -0700