2011-01-20 13 views
0

私はUISegmentedcontrolと2つのオプションを持つUINavigationcontrolを持っています。 2つのオプションは異なるUIViewコントローラーにプッシュします。ユーザが2番目のオプションを押すと、UISegmentControlはまだそこにありますが、ユーザが最初のオプションを再び押すとUISegmentControlは消えます。そこにはどんなコードが必要ですか?UISavmentedcontrol with UINavigationcontrol - 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は、私が2番目のセグメントを押すと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 /メートルで同じコードを使用し

が、今あなたのスニペットを試してみたところで、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であるべきものを外部化していないと思います。その代わりに、スタック内の各View ControllerにviewWillAppearを実装して、正しいtitleViewを独自のnavigationItemに配置します。あなたは

Step3.m上記の参考文献ステップ3クラスで例えば...

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

これを行うには、すべてのビューコントローラの内部の詳細を知ることからCoreDataMenuAppDelegateから責任を削除します。

また、Model-View-Controllerの話をWWDC 2010から見るのに役立つかもしれません。

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

話はiPhone OSのセッション116モデル - ビュー - コントローラです。

セッション116には、ビューコントローラの考え方とクラス間の機能のデビジョンに関する情報が満載です。具体的には、コントローラクラス間のカプセル化を尊重することがどれほど重要であるかについて説明します。

+0

こんにちはビルドゥドニーとあなたの返信をありがとう!まず第一に、私はかなり私が客観的に新しいことを言って始めます.c。だから私は試行錯誤の方法で学ぶことを試みている;)この小さなコードスニペットよりも何かがあるのだろうか?私はそれを実装しようとしたが、私が得るすべての警告は、 'UIViewController'が '-viewWillAppear'に応答しない可能性があります。ありがとうございました – xqtr

+0

更新、申し訳ありません。単一のBOOLパラメータ –

+0

これに対処する方法のヒントを教えてください。私は間違ったことをしましたか? – xqtr

関連する問題