navigationcontroller causing app to crash - iphone

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

Related

navigate to new page on button click in iPhone

I am new to iPhone Developer.
i want to do navigate to new page with animation but i am unable to navigate to new page,
Here is my code snippet,
UIButton *Planbtn = (UIButton *)[self.view viewWithTag:1];
[Planbtn addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
..
-(void)btnClicked{
NSLog(#"btnClicked");
PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:#"PlanPage" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:yourViewController animated:TRUE];
[yourViewController release];
}
i am able to see btnClicked in my Log.
Thanks in Advance !
Write these properties in AppDelegate.h file
#property (strong, nonatomic) YourFirstViewController *yourFirstController;
#property (nonatomic, retain) UINavigationController *navigationControl;
Write this code in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.yourFirstViewController = [[YourFirstViewController alloc] initWithNibName:#"YourFirstViewController" bundle:nil]];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.yourFirstViewController];
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
I think it will be helpful to you.
Being your app view based, you can't push the controller, the simplest thing to do is:
PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:#"PlanPage" bundle:[NSBundle mainBundle]];
[self.view addSubView:yourViewController.view];
[yourViewController release];
If you want to animate it, I'm afraid you have either to implement the animation by yourself, or to add a navigation controller to do the job for you

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

dismissing modalview is not working

i m presenting modalviewcontroller without using delegate protocol. But want to dismiss modalviewcontroller using delegate protocol.
Basically i m pushing modalviewcontroller like this
- (void)displayModalViewaction: (id) sender
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[Infoviewcontroller alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
[_viewController release];
[navigationController release];
}
For dismissing modalview doing this
In the ModalViewController.h delegated protocol and method
#protocol ModalViewDelegate <NSObject>
-(void) dismissModalView:(UIViewController *) viewController;
#end
#interface Infoviewcontroller : UIViewController <ModalViewDelegate>
{
id<ModalViewDelegate> dismissDelegate;
}
#property (nonatomic, retain) id<ModalViewDelegate> dismissDelegate;
#end
In modalviewcontroller. m file
#synthesize dismissDelegate;
-(void) dismissModalView:(UIViewController *) viewController;
{
[self dismissModalViewControllerAnimated:YES];
}
#end
-(void) dismissView: (id)sender
{
[delegate dismissModalView:self];
}
-(void) dismissModalView:(UIViewController *) viewController;
{
[self.dismissModalViewController Animated:YES];
}
#end
But somehow it is not working when clicking on done button
UIButton* backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Done" forState:UIControlStateNormal];
// create button item
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add the button to navigation bar
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
Anyone have any clue that what i m missing in my code or what i m doing wrong. Help will be really appreciated.
Thanks
I would have liked to comment, but not enough privilege. Anyways AFAIK...
One does not 'push' a modal VC... it needs to be 'presented' like this..
[navigationController presentModalViewController:_viewController animated:YES];
Only a presented Modal VC will get dismissed on calling [self dismissModalViewControllerAnimated:YES];
OR, in case you really need to 'push' it.. then you need to 'pop' it to get back!
Hope it helps
Correct me if I'm wrong but wouldn't you want to define _viewController in your .h file and dismiss it with: [delegate dismissModalView:_viewController]; rather then dismissing self because self is not a view controller.
You are pushing onto the navigation's stack so no Modal was ever displayed you need to pop the view off the stack:
[self.navigationController popViewControllerAnimated:YES];
instead of:
[self dismissModalViewControllerAnimated:YES];

NavigationController not showing on UIViewController presenting modally

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

Presenting Modalviewcontroller for Info Button when pressed on UIToolbar programmatically

I want is on main window to present the created modalViewController view when the infobutton is pressed. But when I press Info button on the main window nothing happens.
In the mainviewcontroller.h file I have following code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) UIToolbar *toolbar;
#property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#end
In the mainviewcontroller.m file have following code:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
#implementation imageviewViewController
#synthesize toolbar;
#synthesize currentSystemItem;
#synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
Should be
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Infobuttonpressed:)];
Spot the difference? First, you're missing a target (should be self, not nil). Second, the colon at the end of the selector is part of the signature, you were missing it so it was not calling your method (which is InfoButtonPressed:(id)sender).
As an aside, methods should start with a lower case name.
you dont need both pushModalViewController and pushViewController
Assuming your views are already controlled within an instance of a UINavigationController pushModalViewController on its own will work.
edit - remove the reference to pushViewController. Does it now work?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
The target of your infoItem is set to nil. You should set this to an instance of the class where the Infobuttonpressed is defined. I assume that is the mainviewController?
Also, the selector to call is specified as: #selector(Infobuttonpressed), while the method is called "Infobuttonpressed:", that is, with a parameter. That will likely result in a runtime error once you fix the target.
Then, there are some peculiarities in the way you handle things. As Nik already answered, you need to choose whether you want to present the view modally (with presentModalViewController), or present it as part of the navigation stack (with pushViewController), you cannot do both.
If you present it as part of the navitation stack, then it already has a navigation bar, and so you should not add one yourself. Just set the properties of the self.navigationItem.
Have a look at the View Controller Programming Guide for more info.