NavigationController not showing on UIViewController presenting modally - iphone

Have a mainviewcontroller and on it have UIToolbar which has a UIBarButtonItem Info which shows UIViewcontroller modally.
Now when pressing Infobutton it is showing UIViewController modally which has UITextView but not showing UINavigationController with Done button.
I am not able to figure it out what i am missing in my code.
This is how i am showing UITextView and NavigationController in UIViewController modally.
#import "ModalViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation ModalViewController
#synthesize textView;
#synthesize navBar;
#synthesize navigationController;
#synthesize delegate;
-(void)dealloc
{
[textView release];
[navBar release];
[navigationController release];
[super dealloc];
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.title = #"Info";
UINavigationController *navigationController = [[UINavigationController alloc]init];
//initWithRootViewController:viewController];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(Done:)] autorelease];
self.textView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)]autorelease];
self.textView.textColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Georgia-BoldItalic" size:14];
//self.textView.delegate = self;
self.textView.backgroundColor = [UIColor brownColor];
self.textView.layer.borderWidth = 1;
self.textView.layer.borderColor = [[UIColor whiteColor] CGColor];
self.textView.layer.cornerRadius = 1;
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.\nThis is UITextView presenting modally.
self.textView.editable = NO;
//[self.view addSubview:navigationController.view];
[self.view addSubview: self.textView];
//[navigationController release];
}
And this is how UIViewController presented modally
//Create a final modal view controller
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self action:#selector(modalViewAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.rightBarButtonItem = modalBarButtonItem;
- (void) modalViewAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//self.viewController = [[ModalViewController alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//ModalViewController *myModalViewController = [[ModalViewController alloc] init];
//UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myModalViewController];
//navigationController.navigationBar.tintColor = [UIColor brownColor];
_viewController = [[ModalViewController alloc] init];
//[navigationController pushViewController:_viewController animated:YES];
[self presentModalViewController:self.viewController animated:YES];
//[self.view addSubview:navigationController.view];
//[navigationController release];
[myModalViewController release];
}
I will appreciate if you can figure out what i m doing wrong or missing in my code.
Thanks a lot.

This seems like a needlessly convoluted way of displaying the new controller. In my apps, I do it like this:
//this function displays (modally) view controller nested inside a navcontroller
- (void) showModalController
{
YourViewController * ecreator = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
UINavigationController * navcontrol = [[UINavigationController alloc] initWithRootViewController: ecreator];
[self presentModalViewController: navcontrol animated:YES];
[navcontrol release];
[ecreator release];
}
Now you want to do the graphical setup (navbar color etc) in the initWithNib and/or viewDidLoad functions of the YourViewController.

#synthesize navigationController;
so navigationController is your class member variable.
in the function
- (void) viewDidLoad
you declare a local variable
UINavigationController *navigationController
Please notice that the second navigationController is different from your member variable navigationController .
So, inside viewDidLoad you need to create object of your member variable navigationController. Not the local variable navigationController.
Do not RE-declare navigationController in viewDidLoad. Instead, create the object using member variable like:
navigationController = [[UINavigationController alloc]init];

Try this, it worked great for me. I had the exact same problem
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#“segue_name"])
{
UINavigationController *nav = [segue destinationViewController];
ExampleViewController *exampleVC = (ExampleViewController *) nav.topViewController;
//Setup any properties here and segue
}
}

Related

navigationcontroller causing app to crash

I have added navigation controller into my appication in didFinishLaunchingWithOptions like this
LoginViewController *mainView = [[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc]initWithRootViewController:mainView]autorelease];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
And in my LoginViewController's viewDidLoad i have,
self.navigationItem.hidesBackButton = YES;
[super viewDidLoad];
UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:#"top_bar.png"];
bar = [self.navigationController navigationBar];
[bar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
UIImage *signIn = [UIImage imageNamed:#"signin_btn.png"];
UIButton *phButton = [UIButton buttonWithType:UIButtonTypeCustom];
[phButton setImage:signIn forState:UIControlStateNormal];
phButton.frame = CGRectMake(0.0, 0.0, signIn.size.width, signIn.size.height);
UIBarButtonItem *phBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:phButton];
self.navigationItem.rightBarButtonItem = phBarButtonItem;
[phButton addTarget:self action:#selector(checkConnection) forControlEvents:UIControlEventTouchUpInside];
[phButton release];
When i run the app in my phone it crashes. When i remove the navigationcontroller from the appDelagate it works... Y cant i get my navigation to work properly and how can i avaoid it from getting crashed.
Modify your code
try this code
Declare appdelete.h file
#class LoginViewController;
LoginViewController *viewController;
#property (nonatomic, retain) IBOutlet LoginViewController *viewController;
appdelegate.m file declare
#synthesize viewController;
in didFinishLaunchingWithOptions
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:nav.view];
[window makeKeyAndVisible];
return YES;
-(void) dealloc
{
[viewController release];
......//some code
}
I this this cause crash.
[phButton release];
phButton does not need to be released;
By the way,
phBarButtonItem need to be released;
Try this one...
self.window.rootViewController = navigationController;
instead of [self.window addSubview:navigationController.view];
[phButton release]; remove that line &
img & phBarButtonItem needs to be released
I think the problem is with your autorelease with navigation controller.
you try removing autorelease or
navigationController = [[UINavigationController
alloc]initWithRootViewController:mainView];
try using property (if you have),
self.navigationController = [[[UINavigationController
alloc]initWithRootViewController:mainView]autorelease];

Transitionstyle of UIViewController presented modally

I am using the below statement to define the transitionstyle
_viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
in the below code but the above statement is not flipping horizontally while presenting it modally
#property (nonatomic, assign) UIModalTransitionStyle *modalTransitionStyle;
#synthesize modalTransitionStyle;
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_viewController = [[ModalViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
[_viewController.navigationItem setLeftBarButtonItem:button animated:YES];
navigationController.navigationBar.tintColor = [UIColor brownColor];
_viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:_viewController animated:YES];
Anyone knows that why it is so. When everthing is coded programmtically.
Thanks for help though.
There's a lot of confusing naming going on here.
AFAICT, _viewController is the root view controller of your navigationController. But you reference self.viewController... is this the same view controller? If so, why are you not consistently using accessor methods? If not, it's not clear to me that you setting the modal transition style on the correct view controller ([self viewController] vs. _viewController).
(Btw, please work on formatting your code so that it displays in a more reasonable way when you paste it in.)
Wrong way to approach the problem. I suggest you to review your code in order to improve readability and maintainability.
Supposing that you've a viewcontroller with a target action associated with a button, something like this:
...
[aButton addTarget:self
action:#selector(dismissView:)
forControlEvents:UIControlEventTouchUpInside];
...
You can write a selector method, in order to display your new viewcontroller as follow:
- (void)dismissView:(id)sender {
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:secondVC animated:YES];
[secondVC release];
}

Uitabbarcontroller in a UIViewController not in the appdelegate

How do I integrate a UITabbarcontroller in a UIViewController class not in the app delegate? I was suppose to make a login view and after it the UITabBarController appears which was created in a UIViewController class? Can anyone suggest what needs to be done? thanks
You can still put the UITabBarController in the App Delegate, when the login is done, just tell the app delegate, and switch the them:
self.window.rootViewController=tabBarController;
if your Application is Navigation Based App, then Create TabBarController(with ViewControllers how many you want to add) and add it on Navigation Controller, like this
UITabBarController *tabBarController = [Utility configureMessagesTabBArController];
self.navigationController.navigationBarHidden=YES;
[self.navigationController pushViewController:tabBarController animated:YES];
[tabBarController release];
here is configureMessagesTabBArController Method from Utility class
+(UITabBarController *)configureMessagesTabBArController
{
UITabBarController *tabBarController = [[UITabBarController alloc]init];
AktuellesViewController *aktuelles_Controller = [[AktuellesViewController alloc]init];
TermineViewController *termine_Controller = [[TermineViewController alloc]init];
TopTenViewController *topTen_Controller = [[TopTenViewController alloc]init];
MediathekViewController *mediathek_Controller = [[MediathekViewController alloc]init];
KontaktViewController *kontakt_Controller = [[KontaktViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:aktuelles_Controller];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:termine_Controller];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:topTen_Controller];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:mediathek_Controller];
UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:kontakt_Controller];
nav1.navigationBar.tintColor = [UIColor blackColor];
nav2.navigationBar.tintColor = [UIColor blackColor];
nav3.navigationBar.tintColor = [UIColor blackColor];
nav4.navigationBar.tintColor = [UIColor blackColor];
nav5.navigationBar.tintColor = [UIColor blackColor];
[tabBarController setViewControllers:[[NSArray alloc]initWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
[nav1 release];
[nav2 release];
[nav3 release];
[nav4 release];
[nav5 release];
[aktuelles_Controller release];
[termine_Controller release];
[topTen_Controller release];
[mediathek_Controller release];
[kontakt_Controller release];
return tabBarController;
}

UIToolbar and UINavigationController

I need to display a view modally. The viewcontroller that needs to be displayed modally has to have a UIToolbar at the bottom. In this toolbar there are one uisegmentedcontroller with three elements. (Think of an tabbar).
In the viewcontroller that presents the modal viewcontroller I have:
-(IBAction)presentModally:(id)sender {
if (self.nvc == nil) {
MyModalViewController *vc = [[MyModalViewController alloc] init];
UINavigationController *navvc = [[UINavigationController alloc] initWithRootViewController:vc];
navvc.navigationItem.prompt = #"";
navvc.navigationBar.barStyle = UIBarStyleBlack;
[vc release];
self.nvc = navvc;
[navvc release];
}
[self presentModalViewController:self.nvc animated:YES];
}
MyModalViewController:
- (void)loadView {
[super loadView];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
uiview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = uiview;
[uiview release];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 436.0f, 320.0f, 44.0f)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segCon = [[UISegmentedControl alloc] initWithItems:itemArray];
segCon.frame = CGRectMake(60, 4, 200, 36);
segCon.segmentedControlStyle = UISegmentedControlStyleBar;
segCon.tintColor = [UIColor darkGrayColor];
segCon.selectedSegmentIndex = 0;
[segCon addTarget:self action:#selector(changedSegment:) forControlEvents:UIControlEventValueChanged];
[toolbar addSubview:segCon];
self.segmentedControl = segCon;
[segCon release];
[[self navigationController].view addSubview:toolbar];
[toolbar release];
}
- (void)changedSegment:(id)sender {
UISegmentedControl *control = (UISegmentedControl *)sender;
int index = control.selectedSegmentIndex;
[[self navigationController] popViewControllerAnimated:NO];
[[self navigationController] pushViewController:[self.controllers objectAtIndex:index] animated:NO];
}
The viewcontrollers in the array are just normal UIViewControllers.
I have set this property in those classes to:
self.navigationItem.hidesBackButton = YES;
My question: Is this the proper way to achieve a UITabBarController behavior?
Haven't tested it, but It looks good, except that popViewControllerAnimated. I'd use popToRootViewControllerAnimated instead (In case a controller uses itself the navigationController).

NavigationBar is not appearing on my ViewController

I have a UIViewController that is presented modally. The UIViewController is inside of a UINavigationController.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
saveButton = [[UIBarButtonItem alloc] initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(saveButtonClicked:)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
self.title = #"Login";
//toolbar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
}
Why isn't my navbar appearing with the text Login and a save button to the right?
It sounds like you may be presenting the view controller instead of the navigation controller. Your code to present the view controller you've described should look something like this:
MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];
[viewController release];
[navController release];
You set the properties after the nib is loaded, but you probably need to do it after/while it is pushed.