2013-10-11 468 views
0

我上的appDelegate當前的應用程序加載main.xib屏幕僅包含兩個圖像背景和標誌。代碼後面的這個屏幕確定用戶是否登錄系統,如果不是,它將顯示登錄,否則它將顯示儀表板。iOS應用具有多個視圖控制器

的應用已被創建爲單個視圖應用,示例代碼的appDelegate的:

// Override point for customization after application launch. 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
{ 
    self.Main = [[vcMain alloc] initWithNibName:@"vcMain" bundle:nil]; 
    self.window.rootViewController = self.Main; 
    [self.window makeKeyAndVisible]; 
} 
else 
{ 
    self.MainiPad = [[vcMain_iPad alloc] initWithNibName:@"vcMain_iPad" bundle:nil]; 
    self.window.rootViewController = self.MainiPad; 
    [self.window makeKeyAndVisible]; 
} 

在的main.m上viewDidLoad中我有以下:

if (islogged) 
{ 
    vcDashboard *vcDash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil]; 
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcDash]; 
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    _ncMain.view.frame = self.view.bounds; 
    [self.view addSubview:_ncMain.view]; 
    ViewActive = vDash; 
} 
else 
{ 
    vcLogin *login = [[vcLogin alloc] initWithNibName:@"vcLogin" bundle:nil]; 
    login.modalPresentationStyle = UIModalPresentationFormSheet; 
    login.view.frame = self.view.bounds; 
    [self presentViewController:login animated:YES completion:nil]; 
} 

有在信息中心提供的菜單按鈕向用戶呈現一系列選項中選擇另一個畫面,按下時會激活以下方法:

- (void)displayView:(NSString *)strView Notification:(NSNotification *)notification{ 

if(_ncMain) 
{ 
    [_ncMain.view removeFromSuperview]; 
    _ncMain = nil; 
} 

if ([strView isEqual: @"Dashboard"]) 
{ 
    vcDashboard *vcDash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil]; 
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcDash]; 
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    _ncMain.view.frame = self.view.bounds; 
    [self.view addSubview:_ncMain.view]; 
    ViewActive = vDash; 
} 
else if ([strView isEqual: @"Catalog"]) 
{ 
    vcCatalog *vcCat = [[vcCatalog alloc] initWithNibName:@"vcCatalog" bundle:nil]; 
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcCat]; 
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    _ncMain.view.frame = self.view.bounds; 
    [self.view addSubview:_ncMain.view]; 
    ViewActive = vCatalog; 
} 
else if ([strView isEqual: @"News"]) 
{ 
    vcNews *vcNew = [[vcNews alloc] initWithNibName:@"vcNews" bundle:nil]; 
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcNew]; 
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    _ncMain.view.frame = self.view.bounds; 
    [self.view addSubview:_ncMain.view]; 
    ViewActive = vNews; 
} 

}

我懷疑這裏是我好像不知道這是當一個選項是從該菜單中選擇屏幕之間切換的正確方法,如果是正確的,總是addSubview到主屏幕。不知道使用導航控制器模板是否是一種解決方案。我擔心應用程序在完成所有這些時所消耗的內存,同時我也正在項目中使用ARC。

+0

看看ios中的視圖遏制模式。 Viewcontrollers可以有自己的視圖的子視圖控制器。它是一種將你的應用打破成可插拔組件的非常整潔優雅的方式 –

回答

0

我建議你避免addSubview方法如果可能的話。 UiNAvigationController爲您提供了一種處理不同viewController的好方法。例如,如果您使addSubview發生changeRotation事件,則不會調用該事件。當你做出一個流行的viewController是dealloced。

祝你好運!

+0

我必須使用將NavigationController添加到AppDelegate中的self.window而不是self.Main中嗎? – Angie

+0

self.Main = [[vcMain alloc] initWithNibName:@「vcMain」bundle:nil]; UINavigationController * nvController = [UInavigationController alloc] initwithRootViewController:self.Main]]; self.window.rootViewController = nvController; [self.window makeKeyAndVisible]; – Angie

+1

是的,你必須從相同的東西開始:'self.homeViewController = [[HomeViewController alloc] initWithNibName:@「HomeViewController」bundle:nil]; self.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; self.window.rootViewController = self.mainNavigationController; [self.window makeKeyAndVisible]' –