如何从 appdelegate 显示 uiviewcontoller

Posted

技术标签:

【中文标题】如何从 appdelegate 显示 uiviewcontoller【英文标题】:How to make uiviewcontoller display from appdelegate 【发布时间】:2014-06-13 19:28:58 【问题描述】:

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

这是 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;

【问题讨论】:

为什么要将标签栏控制器推到导航控制器上?每个选项卡都应根据需要有自己的导航控制器。 @rmaddy 我删除了该行,但它仍然无法正常工作 “不起作用”这些词是开发人员可以使用的最无用的词。这是没有意义的。请更新您的问题,详细说明您想要发生的事情并解释实际发生的事情。具体一点。 我没有看到您将 tabBarController 添加到任何地方的窗口中。尝试将return YES 之前的最后两行替换为self.window.rootViewController = tabBarController 【参考方案1】:

.h

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

.mappDidFinish

// 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];

也许这会对你有所帮助。

【讨论】:

有没有办法让标签栏项目一次全部显示?因为在我设置了所有项目之后,当我运行时,只显示第一个项目标题。因此,要出现其他 tabbaritems,我实际上必须单击每个选项卡才能看到它下面的标题。 那么列出所有 tabbaritems 的应用程序怎么样?您不必单击每个选项卡,然后它会显示它下面的标题,它在首次加载应用程序时已经存在。你明白我的意思吗?【参考方案2】:

Main.storyBoard 中实例化一个UITabBarController,并将其连接到身份检查器中的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

【讨论】:

以上是关于如何从 appdelegate 显示 uiviewcontoller的主要内容,如果未能解决你的问题,请参考以下文章

如何从 appdelegate 显示 uiviewcontoller

从 UIView 子类推送导航控制器会导致崩溃

如何在 Swift 中 3 秒后从 AppDelegate 中删除FromSuperview

将 UIView 添加到横向窗口

IOS 从 AppDelegate 中的 addMessageFromRemoteNotification 显示 UIViewController

与 UIView 子类相关的 UIView 元素显示在其容器之外