无需添加appDelegate文件即可创建UITabBar

时间:2012-07-18 06:26:45

标签: iphone objective-c ios xcode uitabbar

我正在制作一个有3页的应用程序

  1. 登录页面 - 应用加载后的第一页
  2. 我的第一页 - 当用户成功登录后,他进入此页面。此页面
    包含一个带有两个UITabBarItem的UITabBar。第一个连接到

    我的第一页

    和另一个到我的第二页。

  3. 我的第二页 - 这是另一个UIViewController。

  4. 我已经创建了登录页面,但是我无法找到UITabBar在My First Page中添加的解决方案

    请帮帮我

4 个答案:

答案 0 :(得分:0)

试试这个,

假设这是LoginViewController.m

-(IBAction)loginButtonClicked:(id)sender
{
    [self createTabBarView];
}

//Create TabBar View here 
-(void)createTabBarView
{
     NSMutableArray *tabItems = [NSMutableArray array];
     UIViewController *firstViewController = [[FirstViewController alloc] init];;
     firstViewController = @"First View";
     firstViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
      [tabItems addObject:firstViewController];
      UIViewController *secondViewController = [[SecondViewController alloc] init];;
      secondViewController = @"Second View";
      secondViewController = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1];
      [tabItems addObject:secondViewController];
      self.tabBarController = [[UITabBarController alloc]init];
      self.tabBarController.viewControllers = tabItems;    
      self.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
      [self presentModalViewController:tabBarController animated:YES];
}

谢谢,

尼基尔。

答案 1 :(得分:0)

定义 AppDelegate.h

@property (strong, nonatomic) UITabBarController *tabBarController;
AppDelegate.m中的

didFinishLaunchingWithOptions

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
self.tabBarController.delegate=self;


 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

return YES;
}

现在当你成功登录时,在该方法中写下代码

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    delegate.tabBarController = [[UITabBarController alloc] init];
    delegate.tabBarController.selectedIndex = 0;
    delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self.navigationController pushViewController:delegate.tabBarController animated:YES];

答案 2 :(得分:0)

您使用的是界面构建器吗? 从我的角度来看,我更愿意使用编程方式来实现它。

//In the appDidFinishLaunch method

    BOOL loggedIn = [[NSUserDefault standDefault]boolForKey:"userlogin"];

    if(loggedIn)
    {

        //Setup your UITabbarViewController

    }
    else
    {

        //Setup your loginView Controller
    }

登录LogInViewController后

- (void)didLogin
{

   YourAppDelegate *delegate = [UIApplication shareApplication].delegate;  

   [delegate.window addSubView:self.tabBarViewController.view];

   [delegate.window removeSubView:self.view]

   //Other Config

}

答案 3 :(得分:0)

您应该在可以识别登录成功完成的位置创建Tabbar。此方法应该是loginViewController的一部分。 创建一个如下所示的函数来创建一个Tabbar并通过loginController呈现它。

-(void) createTabBarController
{
     UITabBarController *tabBar = [[UITabBarController alloc]init];

     UIViewController *firstViewController = [[[UIViewController alloc] init]autorelease];
     UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
     firstNavController.tabBarItem.title=@"First Controller";
     firstNavController.tabBarItem.image = [UIImage imageNamed:@"first.png"];

     UIViewController *secondViewController = [[[UIViewController alloc] init]autorelease];
     UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
     secondNavController.tabBarItem.title=@"First Controller";
     secondNavController.tabBarItem.image = [UIImage imageNamed:@"first.png"];

     [tabBar setViewControllers:[NSArray arrayWithObject:firstNavController,secondNavController,nil]];    
     [firstNavController release];
     [secondNavController release];

     [self presentModalViewController: tabBar];
     [tabBar release];
}