开发者

Go to another view (UIViewController) from within UIViewController

开发者 https://www.devze.com 2023-03-08 17:03 出处:网络
A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.

A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.

In my ViewDidLoad I have a condition that if thats not met, load the second view instead.

Is there any kind soul that c开发者_JAVA百科an help out a Objective-C beginner? I have googled and search stackoverflow for the answer but with no luck.

FirstViewController.h

@interface FirstViewController : UIViewController {

some variables

}

some @properties

FirstViewController.m

#import "FirstViewController.h"

@implementation FirstViewController

- (void)viewDidLoad
{
    if(condition is met) {
        [self.tabBarController setSelectedIndex:1];
    }
}

In my AppDelegate.h

#import <UIKit/UIKit.h>

@interface otpAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>     {

UIWindow *window;
UITabBarController *tabBarController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;


@end

AppDelegate.m

@implementation otpAppDelegate

@synthesize window;
@synthesize tabBarController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];

return YES;
}

Edit! I found a solution to my problem.

int selectedindex = 1;
self.tabBarController.selectedIndex = selectedindex;
UIViewController *tempvc = [[self.tabBarController viewControllers] objectAtIndex:selectedindex];
[self.view removeFromSuperview];
[self.view addSubview:tempvc.view];

Did the trick.


Try setting the index for the tabBarController instance of the app delegate like yourAppDelegate.tabBarController


If you are using tabBarController as IBOutlet , there is no need to allocate it in your app delegate. Please remove these code from your appDelegate method (application: didFinishLaunchingWithOptions)..

 tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号