如何从appdelegate制作uiviewcontoller

时间:2014-06-13 19:28:59

标签: ios objective-c uinavigationcontroller uitabbarcontroller appdelegate

我正在使用uitabbarcontoller(带有5个uiviewcontrollers)和uinavigation bar的应用程序。 我在应用程序中创建了所有这些,但是当我运行它时,它不起作用

这是AppDelegate.m类:

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

    HomePageView *viewController = [[HomePageView alloc] init];
    FeedViewController *feedViewController=[[FeedViewController alloc]init];
    ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
    PlayViewController *playViewController = [[PlayViewController alloc]init];
    ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
    RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];

    tabBarController.viewControllers=[NSArray arrayWithObjects:feedViewController,profileViewController,playViewController,listeningSessionViewController,recievedViewController, nil];

    //navigating to the UITabBarController that you created
    [navCon pushViewController:tabBarController animated:YES];
    [navCon pushViewController:viewController animated:NO];

    return YES;
}

2 个答案:

答案 0 :(得分:2)

.h

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navC;
@property (strong, nonatomic) UITabBarController *tabC;

appDidFinish

中的 .m
// Initialize window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

// Initialize your five tab controllers.  with each tab has its own navigation controller
FeedViewController *feedViewController=[[FeedViewController alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:feedViewController];

ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];

PlayViewController *playViewController = [[PlayViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:playViewController];

ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:listeningSessionViewController];

RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];
UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:recievedViewController];

// initialize tabbarcontroller and set your viewcontrollers.
self.tabC = [[UITabBarController alloc]init];
self.tabC.viewControllers=[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5, nil];

// Inititalize Navigationcontroller and set root as tabbar.
self.navC = [[UINavigationController alloc]initWithRootViewController:self.tabC];

// Set Window rootview as navigation.
self.window.rootViewController = self.navC;

// Show window
[self.window makeKeyAndVisible];

也许这会对你有帮助。

答案 1 :(得分:1)

UITabBarController中实例化Main.storyBoard并将其连接到身份检查器中的班级tabBarCon

接口:

@interface tabBarCon: UITabBarController

@end

实现:

@implementation tabBarCon

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

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   // Do any additional setup after loading the view.

   HomePageView *viewController = [[HomePageView alloc] init];
   FeedViewController *feedViewController=[[FeedViewController alloc]init];
   ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
   PlayViewController *playViewController = [[PlayViewController alloc]init];
   ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
   RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];


        NSArray *viewControllerArray = [[NSArray alloc]initWithObjects:viewController,
                                                                       feedViewController,
                                                                       profileViewController,
                                                                       playViewController,
                                                                       listeningSessionViewController,
                                                                        recievedViewController,nil];

//Then add buttons
 UITabBarItem *moreItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMore tag:0];
 viewController.tabBarItem = moreItem;
//...


 self.viewControllers = viewControllerArray;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end