dismissing modalview is not working - iphone

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

Related

PushingViewController from code

I'm having a problem switching between 2 view controllers. I have a supporting class called myViewController.
I want to click on a button in my main ViewController.m and go to this view.
I've looked on this site and found some code but can't get it to work in my code.
ViewController.m
#import "myViewController.h"
-(void)viewDidLoad
{ //create button that will switch views
UIButton *_buttonAccount = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_buttonAccount setTitle:#"Create Account" forState:UIControlStateNormal];
_buttonAccount.frame = CGRectMake(110, 350, 95, 30);
[_buttonAccount addTarget:self action:#selector(send:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_buttonAccount];
}
-(void) send:(id)sender{
myViewController *myViewController = [[myViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self presentModalViewController:myViewController animated:YES];
[myViewController release];
}
Try this one
-(void) send:(id)sender
{
myViewController *myViewController = [[myViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:passview animated:NO];
}
try this one really helpful to you.
Use this to present the VC
[self presentViewController: myViewController animated:YES completion:^{
}] ;

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

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

Can't add UINavigationController programmatically to a TabBar Application

I have an application for which I use TabBar template. In one of viewcontrollers I want to add a uinavigationcontroller. I declare it in the .h file;
#import <UIKit/UIKit.h>
#import "AnotherViewController.h"
#interface SecondViewController : UIViewController <UINavigationControllerDelegate> {
UIButton *UIButton *gotoAnotherView;;
AnotherViewController *anotherView;
UINavigationController *navigationController;
}
#property(nonatomic,retain) UIButton *UIButton *gotoAnotherView;;
#property(nonatomic,retain) AnotherViewController *anotherView;
#property(nonatomic,retain) UINavigationController *navigationController;
-(void)buttonPressed:(id)sender;
#end
And here's my .m file
#import "SecondViewController.h"
#implementation SecondViewController
#synthesize navigationController, anotherView, gotoAnotherView;
-(void)buttonPressed:(id)sender {
anotherView = [[AnotherViewController alloc]init];
[navigationController pushViewController:anotherView animated:YES];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
navigationController = [[UINavigationController alloc ]initWithRootViewController:self];
[navigationController.navigationBar setFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navigationController.navigationBar];
gotoAnotherView = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 40, 40)]; //kategoributonlari
UIImage *image = [UIImage imageNamed:#"1.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(110, 5, 100, 20);
[self.view addSubview:imageView];
[kategori1 setBackgroundImage:image forState:UIControlStateNormal];
[kategori1 addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:kategori1];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
However I can see from the navigation bar that the navigationcontroller goes one level deeper(back button appears) but the main view remains the same with my gotoAnotherView button.
I think that I might not make the navigationcontroller control the whole view.
Instead of trying to do this in code, edit the XIB for your main window (with the UITabBarController). Drag out a UINavigationController from the Library onto the tab bar. This will create a new bar item for you, with a UINavigationController. Select the UIViewController nested in the new UINavigationController, and on the Identity tab set the Class to your view controller, and on the Attributes tab specify the name of the nib file to load.
You don't need to use IB. You can setup everything in code. First create your view controllers tab1ViewController, tab2ViewController, etc. then create the navigation controller with the root view controllers of tab1ViewController etc. and then add these controllers to the tab bar controller.
Here is a sample:
UINavigationController *tab1NavigationController = [[UINavigationController alloc] initWithRootViewController:tab1ViewController];
UINavigationController *tab2NavigationController = [[UINavigationController alloc] initWithRootViewController:tab2ViewController];
UITabBarController rootViewController = [[UITabBarController alloc] init];
rootViewController.viewControllers = [NSArray arrayWithObjects:tab1NavigationController, tab2NavigationController, nil];
[tab1NavigationController release];
[tab2NavigationController release];

UITabBarController rotation problem with popViewControllerAnimated and selectedIndex (iPhone SDK)

This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].