เลือกแท็บที่สองจาก ModalViewController

ในผู้รับมอบสิทธิ์แอปของฉัน ฉันโหลดตัวควบคุมมุมมองที่ด้านบนของแถบแท็บ คอนโทรลเลอร์นี้มีปุ่มอยู่สามปุ่ม หนึ่งปุ่มสำหรับนำทางไปยังแต่ละแท็บ เมื่อกดปุ่มที่สอง ฉันต้องการยกเลิกตัวควบคุมมุมมองและไปที่แท็บที่สอง แต่ดูเหมือนว่าจะไม่ทำงานตามปกติ

ตัวแทนแอปของฉัน:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    //-- Insert a delay of 5 seconds before the splash screen disappears
    [NSThread sleepForTimeInterval:3.0];        

    // Set the tab bar controller as the window's root view controller and display.
    self.window.rootViewController = self.tabBarController;

    // Set StartView to load first
    StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
    [window addSubview: [startViewController view]];
    [window makeKeyAndVisible];

    [self.tabBarController presentModalViewController:startViewController animated:NO];
    [startViewController release];

    return YES;
}

และนี่คือ IBAction ปัจจุบันของฉันซึ่งดูเหมือนจะไม่ทำงาน:

 - (IBAction) toSecondView:(id)sender
    {
    // Show status bar
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    [(UITabBarController *)self.parentViewController setSelectedIndex:1];

    [self dismissModalViewControllerAnimated:NO];
}

ฉันก็ลองสิ่งเหล่านี้เหมือนกัน แต่ไม่ประสบความสำเร็จ:

self.tabBarController.selectedIndex = 1;  

และ

[self.tabBarController setSelectedIndex:1];

ใครสามารถช่วยฉันและอธิบายสิ่งที่ฉันขาดหายไป?


person user1227927    schedule 23.02.2012    source แหล่งที่มา
comment
มันเกิดขึ้นเนื่องจาก คุณได้เพิ่ม viewcontroller ลงบนหน้าต่างเป็น subView แล้วนำเสนอ viewController นั้นบน tabBarController   -  person Kamar Shad    schedule 27.02.2012


คำตอบ (1)


สิ่งนี้เกิดขึ้นเนื่องจากเหตุผลด้านล่าง

คุณได้เพิ่ม ViewController ลงบนหน้าต่างในฐานะ subView ไม่จำเป็นต้องเพิ่ม SubView เนื่องจากคุณได้นำเสนอ ViewController นั้นเป็น ModalViewController แล้ว

โปรดลองตามด้านล่าง

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    
//-- Insert a delay of 5 seconds before the splash screen disappears
[NSThread sleepForTimeInterval:3.0];        

// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;

// Set StartView to load first
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
//[window addSubview: [startViewController view]]; no need to add subView here
[window makeKeyAndVisible];

[self.tabBarController presentModalViewController:startViewController animated:NO];
[startViewController release];
return YES;

}

-(IBAction) toSecondView:(id)sender
{
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
//create delegate's class object for accessing tabBarController
 AppDelegate* delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
 //instead of [(UITabBarController *)self.parentViewController setSelectedIndex:1];
 //delegate.tabBarController your tabBarControler at which you have added viewController
[delegate.tabBarController setSelectedIndex:1];

[self dismissModalViewControllerAnimated:NO];

}

person Kamar Shad    schedule 27.02.2012