2011-01-20 25 views
0

我有UINavigationcontrol和UISegmentedcontrol兩個選項。這兩個選項會推送到不同的UIView控制器。當用戶推送第二個選項時,UISegmentControl仍然存在,但是當用戶再次推入第一個選項時,UISegmentControl消失。我需要哪些代碼?用UINavigationcontrol控制UISegmentedcontrol - UISC消失

CoreDataMenuAppDelegate.h:

#import <UIKit/UIKit.h> 
@interface CoreDataMenuAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { 


    UIWindow *window; 
    UINavigationController *navigationController; 
    UINavigationController *navigationController2; 
    UITabBarController *tabBarController; 
    IBOutlet UISegmentedControl *myMent; 
} 

-(IBAction)segmentAction:(id)sender; 

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

@end 

CoreDataMenuAppDelegate.m:

#import "CoreDataMenuAppDelegate.h" 
    #import "RootViewController.h" 
    #import "Step3.h" 
    #import "Step6.h" 

    @implementation CoreDataMenuAppDelegate 

    @synthesize window; 
    @synthesize navigationController; 
    @synthesize navigationController2; 
    @synthesize tabBarController; 

    -(void)viewDidLoad 
    { 

    [myMent addTarget:self action:@selector(segmentAction:) 
     forControlEvents:UIControlEventValueChanged]; 
    myMent.selectedSegmentIndex = 0 ; 
    } 
    - (IBAction) segmentAction:(id)sender 
UISegmentedControl* segCtl = sender ; 

if([segCtl selectedSegmentIndex] == 0) 
{ 
    [navigationController2 popToRootViewControllerAnimated:NO]; 

//What to put here? 

} 
if([segCtl selectedSegmentIndex] == 1) 
{ 
    NSLog(@"hi this is second segment"); 
    Step6 *step6 = [[[Step6 alloc] initWithNibName:@"Step6" bundle:nil] autorelease]; 
    [self.navigationController2 pushViewController:step6 animated:NO]; 
    step6.navigationItem.titleView = segCtl; 
} 

} 

    - (void)dealloc { 
     [navigationController release]; 
    [navigationController2 release]; 
    [tabBarController release]; 
    [window release]; 
    [super dealloc]; 
    } 

我已經試過:

Step3 *step3 = [[[Step3 alloc] initWithNibName:@"Step3" 
step3.navigationItem.titleView = segCtl; 

,但沒有結果。

當我按下第二個段時,UISegmentControl會顯示何時進入UIViewController,但當我回到第一個段時消失。

有人嗎?

最好的問候, xqtr


歐凱,當我嘗試使用它時,segmentedcontrol從一開始消失。我使用:

Step3.h:

#import <UIKit/UIKit.h> 
@interface Step3 : UIViewController { 
UISegmentedControl * segmentedControl; 
} 
@property (nonatomic, retain) IBOutlet UISegmentedControl * segmentedControl; 
@end 

Step3.m:

#import "Step3.h" 
@implementation Step3 
@synthesize segmentedControl; 

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
self.navigationItem.titleView = self.segmentedControl; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 


- (void)viewDidUnload { 
} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 

我使用完全相同的step3.h/m和step6.h/M相同的代碼,但現在當我嘗試你的片段時,Segmentedcontrol已經在開始視圖中消失(步驟3)。

有什麼建議嗎? :)

回答

0

以下是關於如何更新導航欄的文檔;

http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25

一般我不外部化應該是什麼樣的navigationItem的視圖控制器。相反,您的堆棧中的每個視圖控制器實現viewWillAppear將正確的titleView放置到它自己的navigationItem中。例如,在你上面

Step3.m引用步驟3類...

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    self.navigationItem.titleView = self.segmentedControl; 
} 

這樣做去除CoreDataMenuAppDelegate不必知道你的所有視圖控制器的內部細節的責任。

此外,它可能有助於查看來自WWDC 2010的Model-View-Controller對話;

http://developer.apple.com/videos/wwdc/2010/

的談話是會話116模型 - 視圖 - 控制器爲iPhone OS。

會話116充滿了關於如何思考視圖控制器和類之間功能分解的信息。具體來說,演示者討論尊重控制器類之間的封裝的重要性。

+0

嗨比爾Dudney,謝謝你的回覆!首先,我首先說我對objective-c非常陌生。所以我試圖通過試驗和錯誤方法學習;)除了這個小小的代碼片段之外,還有什麼更多嗎?因爲我試圖實現它,但我得到的是一個警告說:'UIViewController'可能不會響應'-viewWillAppear'最好的問候,xqtr – xqtr 2011-01-22 11:27:33