Pilih tab kedua dari ModalViewController

Di delegasi aplikasi saya, saya memuat pengontrol tampilan di atas bilah tab saya. Pengontrol ini memiliki tiga tombol, satu untuk menavigasi ke setiap tab. Saat tombol kedua ditekan, saya ingin menutup pengontrol tampilan dan membuka tab kedua. Namun hal ini sepertinya tidak berjalan normal.

Delegasi Aplikasi Saya:

- (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;
}

Dan inilah IBAction saya saat ini, yang sepertinya tidak berfungsi:

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

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

    [self dismissModalViewControllerAnimated:NO];
}

Saya mencoba ini juga, tetapi tidak berhasil:

self.tabBarController.selectedIndex = 1;  

Dan

[self.tabBarController setSelectedIndex:1];

Adakah yang bisa membantu saya dan menjelaskan apa yang saya lewatkan?


person user1227927    schedule 23.02.2012    source sumber
comment
itu terjadi karena, Anda telah menambahkan viewcontroller ke jendela sebagai subView dan kemudian menampilkan viewController itu di tabBarController   -  person Kamar Shad    schedule 27.02.2012


Jawaban (1)


Hal ini terjadi karena alasan di bawah ini.

Anda telah menambahkan ViewController Ke Jendela Sebagai subView, tidak perlu menambahkan SubView karena Anda sudah menampilkan ViewController tersebut sebagai ModalViewController.

Silakan Coba seperti di bawah ini.

-(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