Modal viewController dismiss button problem - iphone

I'm creating a simple modal ViewController. I'm creating a nib with a button and on that button press calling a method to display modal viewController in which I'm creating the viewController and a button inside it like this.
UIViewController *modalViewController = [[UIViewController alloc]initWithNibName:nil bundle:nil];
modalViewController.view.backgroundColor = [UIColor redColor];
modalViewController.;
UIButton *btnDismissViewController = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDismissViewController.frame = CGRectMake(60, 160, 150, 50);
[btnDismissViewController setTitle:#"DISMISS" forState:UIControlStateNormal];
[btnDismissViewController addTarget:self action:#selector(dismissViewCOntroller) forControlEvents:UIControlEventTouchUpOutside];
btnDismissViewController.backgroundColor = [UIColor grayColor];
[modalViewController.view addSubview:btnDismissViewController];
[self presentModalViewController:modalViewController animated:YES];
This view is appearing properly but after pressing the button on modalViewController, the target method for dismissing the modalViewController isn't called. I'm definitely missing something obvious but not getting what. Can anybody please help?
Thanx in advance.

I agree with Ole's comment... additionally, make sure you are dismissing it within your dismissViewCOntroller method similar to this:
[self.parentViewController dismissModalViewControllerAnimated:YES];

I think there might be a typo in your code, dismissViewCOntroller seems like it should be dismissViewController, but maybe that was intentional, and the control state should be UIControlEventTouchUpInside.

Related

Back button doesnt work in ios 7

I'm using this code in all ViewControllers to create back button:
self.btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[self.btnBack setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[self.btnBack addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
-(void)cancel:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
But when I go to controller with UIWebView I need top tap 2 times to go back in ios 6.
And in ios 7 when I tap 1 time UIWebView disappeared show black screen with my navigation and on 2nd tap app crashes.
In all screen this works great maybe something special with UIWebView, I dont know.
Help please!
I push webview:
-(IBAction)doPrivacy:(id)sender
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
WebPageViewController *web = (WebPageViewController*)[storyBoard instantiateViewControllerWithIdentifier:#"WebPage"];
web.hidesBottomBarWhenPushed = YES;
web.urlToOpen = #"http://dfdfdf.co";
[self.navigationController pushViewController:web animated:YES];
}
I'm calling the same segue twice, you can fix this by unlinking the connection from the CELL DIRECTLY, to your segue, and having the segue connection originate at the top of the table hierarchy in IB, rather than nested inside the cell. Connect the segue from you View Controller itself, to the segue. If you have done this correct, when you select the segue, it should highlight the ENTIRE view it is coming from, not just the cell.

View did load or View Finished Loading does not run when switching between views?

I am using a custom Switch view controller and you would hope when switching between different views the "view did load" function or "view did finished loading" functions run but they do not.
Here what I am using:
- (IBAction)gotoKeyboardViews:(id)sender
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:#"YellowViewController"
bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
[buttonKeyboard removeFromSuperview];
buttonStart = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonStart.frame = CGRectMake(117,413, 103, 37);
[buttonStart setTitle:#"Restart" forState:UIControlStateNormal];
[buttonStart addTarget:self action:#selector(gotoBlueView:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonStart];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
gotoKeyboardViews suppose to switch the views To YellowView From BlueView...But I think I am a little bit off about implementing the navbar. Please don't tell me to go with the navbar because I don't like their rigidness in design.
The viewWillAppear method is executed as soon as the view gets active again. Maybe that's the hook you are searching?
Reference: Apple UIViewController Class Reference
those methods are only called when the nib file is loaded or the if you've override loadView. They will not get called again unless a new view controller is instantiated.

Custom back button for NavigationController for every screen

I have been trying to subclass the UINavigationController class for modifying things like background image, navigation bar size, back button, tittle font and so on.
After a couple of hours and a lot of reading I have understand that I should not subclass the UINavigationController class...
I would like to have specific design and transfer that design to all screens that use my custom NavigationController...
SO far I haven't fount any elegant solution for this, most of the solutions are based on configuring something for showing in the next screen only (next view).
How can I achieve this? Maybe using categories?
Thanks in advance.
I am showing code for custom leftbarbutton of UINavigationcontroller. You can add this code in each view controller and for both button( right and left)
leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"Yourcutomeimage.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(youraction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease];
//or
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton]autorelease];
Hope, this will help you..
Yes #Nit is right, this is the way to customize the UINavigationController, but if you don't want to do it for every UIViewController you have, you can create a UIViewControllerBase class, which is a subclass of UIViewController and in viewDidLoad method, set all your buttons the way #Nit has described and then make every UIViewController you have to be a subclass of your UIViewControllerBase class.
If you want to add a custom back button to uinavigationcontroller you should use navigationItem.backBarButtonItem and implement it in the class where you initialize the uinavigationcontroller.
Sample :-
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnImage.png" style:UIBarButtonItemStyleBordered target:youtarget action:#sel(your action)];
self.navigationItem.backBarButtonItem = backButton; [backButton release];
I have outlined my solution in this answer: https://stackoverflow.com/a/16831482/171933
Basically, you create a category on UIViewController and call one single method in every viewWillAppear method of your view controllers.

how to determine that the navigation bar's back button has been clicked and then showing a alert

working on a game based project.i am having a view controller as GameScreen.
on the top of Gamescreen having a navigation bar with back button(default).Now if user clicks on back button i have to show alert.
so how to determine "backbutton got clicked."?
any suggestion?
Thanks
In your viewdidload method :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
In the cancel method:
- (IBAction) cancel:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
or show your alert view
}
Otherwise also you can just override the method if you do not want a custom back button.
Your UINavigationBarDelegate has got a nice method for that, called shouldPopItem.
You can override that in your delegate and show there the alert. This gives you also a chance to cancel the going-back (popping).
Stopping the self.navigationItem.leftBarButtonItem from exiting a view - here you go maddy, answers the question in a different context but same basis -

Pressing UIButton pushes new view onto navigation stack

I currently have a window with a view attached that has three UIButtons. When the user presses a button, I would like the view to change to a new view using a NavigationController.
So right now I have the about button coded like so:
UIButton *aboutButton = [UIButton buttonWithType:UIButtonTypeCustom];
aboutButton.frame = CGRectMake(236, 240, 60, 60);
[aboutButton setImage:[UIImage imageNamed:#"About.png"]
forState:UIControlStateNormal];
[aboutButton addTarget:self action:#selector (aboutButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aboutButton];
And for the action function:
-(void)aboutButtonPressed {
UINavigationController *aboutView =[[UINavigationControlleralloc] initWithNibName:#"About" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[self.view addSubview:aboutView.view];
[aboutView release];
}
The About.xib file currently has nothing in it.
Now on to the problem... When the button is pressed a top navigation bar appears at the top but not in the animated sense like I have it coded. It just pops up. Also there is no back button to return to the view with the buttons on them. What am I doing wrong? I can provide more code or details if this is not clear enough.
Edit: Here is a picture to better understand what is happening.
After clicking the about button:
http://dl.dropbox.com/u/1481176/Screen%20shot%202010-12-09%20at%2011.28.40%20AM.png
Edit 2: Removed some wonky code for a back button that shouldn't of been there.
Most of the code after pushViewController looks dodgy.
-(void)aboutButtonPressed {
AboutViewController *aboutView =[[AboutViewController **alloc**] initWithNibName:#"About" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
That should at least push your empty view onto the stack and give you a back button automatically.