0

I have a UINavigationcontrol with UISegmentedcontrol and two options. The two options pushes to different UIViewcontrollers. When the user push the second option the UISegmentControl is still there, but when the user push the first option again the UISegmentControl disappears. What code do I need there?

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

I've tried:

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

but with no results.

The UISegmentControl shows when I go to the UIViewController, when I press the second segment, but disappears when I go back to the first segment.

Anyone?

Best regards, xqtr


Okey, when I try to use it, the segmentedcontrol disappears from the beginning. I use:

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

I use exactly the same code in step3.h/m and step6.h/m, but now when I tried your snippet the Segmentedcontrol disappears already in the start view (step3).

Any suggestions? :)

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24

1 Answers1

0

Here are the docs on how updating the nav bar happens;

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

In general I'd not externalize what should be in the navigationItem for a view controller. Instead have each view controller in your stack implement viewWillAppear to place the correct titleView into it's own navigationItem. For example in the Step3 class you reference above

Step3.m...

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

Doing this removes the responsibility from CoreDataMenuAppDelegate from having to know the internal details of all your view controllers.

Also it might help to view the Model-View-Controller talk from WWDC 2010;

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

The talk is Session 116 Model-View-Controller for iPhone OS.

Session 116 is full of information on how to think about view controllers and the devision of functionality between classes. Specifically the presenter discusses how important it is to respect encapsulation between your controller classes.

Bill Dudney
  • 3,358
  • 1
  • 16
  • 13
  • Hi Bill Dudney and thank you for your reply! First of all, I start off by saying that I'm pretty much new to objective-c. So I'm trying to learn by the trial and error-method ;) Is there anything more to it than this little code snippet? Because I've tried to implement it, but all I get is a warning saying: 'UIViewController' may not respond to '-viewWillAppear' Best regards, xqtr – Joel Wiklund Jan 22 '11 at 11:27
  • updated, sorry i was a bit sloppy on the method signature, it has a single BOOL parameter – Bill Dudney Jan 23 '11 at 15:13
  • Can you please give me a hint how to tackle this? Have I made anything wrong? – Joel Wiklund Jan 25 '11 at 14:46