簡體   English   中英

如何在iPhone中按下UIViewController時更改選項卡項的選定索引

[英]how to change selected index of tabbar item when pushing UIViewController in iPhone

我將一個視圖控制器推到另一個視圖控制器,但是當我推選項卡項索引不變時,如何在將視圖從一個推向另一個時更改所選索引。

試試看

[tabBar setSelectedItem:[[tabBar items] objectAtIndex:index]];

要么

[tabBarController setSelectedIndex:index];

這條線可能會幫助某人

[self.parentViewController.tabBarController setSelectedIndex:0];

嘗試這個:

    [self.tabBarController setSelectedIndex:0];

此處索引0指的是tabbarcontroller的第一個選項卡。

希望能幫助到你.......

為您創建UITabBarController簡單示例。僅創建三個UIViewController代碼可以正常工作:)

appDelegate.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "ViewController.h"
#import "SecoundViewController.h"


@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,UINavigationControllerDelegate>
{
    FirstViewController *fView;
    SecoundViewController *sView;
    ViewController *viewCon;

    UITabBarItem *tbItem1;
    UITabBarItem *tbItem2;
    UITabBarItem *tbItem3;

     UINavigationController *FnavCon;
     UINavigationController *SnavCon;
     UINavigationController *navCon;

     UITabBarController *tbCon;
}

@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,retain)FirstViewController *fView;
@property(nonatomic,retain)SecoundViewController *sView;
@property(nonatomic,retain)UINavigationController *navCon;
@property(nonatomic,retain)UITabBarController *tbCon;
@property(nonatomic,retain)ViewController *viewCon;
@property(nonatomic,retain)UINavigationController *FnavCon;
@property(nonatomic,retain)UINavigationController *SnavCon;
@property(nonatomic,retain)UITabBarItem *tbItem1;
@property(nonatomic,retain)UITabBarItem *tbItem2;
@property(nonatomic,retain)UITabBarItem *tbItem3;

@end

appDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize fView,sView,navCon,tbCon,viewCon,FnavCon,SnavCon,tbItem1,tbItem2,tbItem3;

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

    self.viewCon=[[ViewController alloc] init];
    self.navCon=[[UINavigationController alloc] initWithRootViewController:self.viewCon];
    self.navCon.navigationBar.tintColor=[UIColor blackColor];
    self.viewCon.title=@"First View";

    self.fView=[[FirstViewController alloc] init];
    self.FnavCon=[[UINavigationController alloc] initWithRootViewController:self.fView];
    self.FnavCon.navigationBar.tintColor=[UIColor blackColor];

    self.fView.title=@"Secound View";

    self.sView=[[SecoundViewController alloc] init];
    self.SnavCon=[[UINavigationController alloc] initWithRootViewController:self.sView];
    self.SnavCon.navigationBar.tintColor=[UIColor blackColor];
    self.sView.title=@"Third View";

    UIImage *img1=[UIImage imageNamed:@"Australia.gif"];
    self.tbItem1=[[UITabBarItem alloc] initWithTitle:@"First Page" image:img1 tag:1];
    self.viewCon.tabBarItem=self.tbItem1;

    UIImage *img2=[UIImage imageNamed:@"Cameroon.gif"];
    self.tbItem2=[[UITabBarItem alloc] initWithTitle:@"Secound Page" image:img2 tag:2];
    self.fView.tabBarItem=self.tbItem2;

    UIImage *img3=[UIImage imageNamed:@"Canada.png"];
    self.tbItem3=[[UITabBarItem alloc] initWithTitle:@"Third Page" image:img3 tag:3];
    self.sView.tabBarItem=self.tbItem3;

    NSMutableArray *viewArr=[[NSMutableArray alloc] init];
    [viewArr addObject:self.navCon];
    [viewArr addObject:self.FnavCon];
    [viewArr addObject:self.SnavCon];


    self.tbCon=[[UITabBarController alloc] init];
    self.tbCon.viewControllers=viewArr;

    [self.window addSubview:tbCon.view];

    [self.window makeKeyAndVisible];

    return YES;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM