How do I create a custom back button on one of my storyboards, when I do a segue and I say push it creates a back button on Mac with but when I do a modal or model it does not create a back button?
Modally presented view controllers do not automatically get close buttons.
self.navigationController.leftBarButtonItem = ...
A pushed view controller will automatically create a back button if the navigation controller is shown.
You will have to create your own back button. In the view controller that you have presented via the modal transition, you have to put a toolbar on it. Put it at the top and if using autolayout set the constraints top, both sides and height. Then place a barbuttonitem in the toolbar. You can select a system button like done or cancel. Make sure the new view controller is the class that you created. Now you can control drag from the barbuttonitem to your .h file and connect an IBAction. Call it dismiss or something like that. In that method call [self dismiss viewcontroller:animated completion:nil]. This will bring you back to the original view controller. I am not a my computer right now so I am not sure the exact wording of the dismiss method, but it will auto fill for you. Good luck.
As Douglas explained, you need to create property of the button (UIButton or BarButtonItem) and in the viewcontroller .m file connect
- (IBAction)backButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
with the button. This will dismiss the current view controller and bring you back to the previous one :)
Related
I have an app with several views. Taking into consideration the large main view, called MyView1, it is controlled by MyView1Controller. Within MyView1, there is a button that causes a modal segue to another view, whose controller is also MyView1Controller. This modal view has a couple UILabels, and a button that terminate the modal view, bringing the user back to MainView1.
Here is the problem... Let's say in my modal view there is a UILabel called sampleLabel. While in MyView1, a button is pressed, which executes the code:
sampleLabel.text = #"changed";
Since the UILabel named sampleLabel is not on screen for MyView1, and instead is part of the modal view from MyView1, nothing happens. However, when I click on the button to view the modal view from MyView1, the UILabel hasn't changed.
This is even more puzzling since the main MyView1 and the modal view that segues off of MyView1 are controlled by the same view controller, MyView1Controller.
Can someone please tell me how I can make code that executes during the user's interaction with MyView1 change things in the modal view, so that when they press the button and segue to the modal view, the UILabel's have already been changed?
Thanks!
First of all, Apple recommends (and it makes life a lot easier) to have one view controller for each view. So you should have a second view controller. In the second view controller you would have a property called sampleLabel. In the first view controller you could use different methods to set the sampleLabel.text. I would probably create a separate sampleLabelText property in the first view controller (could be an NSString *) and set it to the text you want when the user presses a button. Then in your
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
you would get your second view controller and set its property like this:
SecondViewController *svc = [segue destinationViewController];
svc.sampleLabel.text = self.sampleLabelText;
That's it. Hope this helps.
So I have had a similar issue that I resolved through 'delegation' but not through a segue schema. Here is a link to the stackoverflow question and my answer. Delegation
Hopefully this gets you going in the right path. Instead of modally presenting a view, I push a new viewcontroller onto a navigation stack but the same answer should apply, hopefully :P
My app dynamically generates buttons. When the button is clicked it creates a table linked to it. I have all the table interfaces (navigation controllers, etc.) set up in storyboard. However, my main view is the home screen where the user creates buttons. Since the button isn't already there, how do I set it up so that it creates a new instance of the tableview outlined in my main storyboard? Is there a "prototype button" in storyboard? The "#selector" action for the generated button is pointing to this IBAction:
-(IBAction)generateTable:(id)sender {
}
Can someone help me fill in the remaining code?
Also, the home screen view controller is already set up in Storyboard, with the arrow next to it.
EDIT: this picture might help understand my problem a better.
http://i.imgur.com/MdT4i1P.png
Found the answer myself quite easily. All you have to do is make a segue (with an identifier) in Storyboard from your ViewController to the ViewController you want it to connect to, then in the IBAction, just add the following.
-(IBAction)generateTable:(id)sender
{
[self performSegueWithIdentifier:#"GoToTable" sender:sender];
}
How can I make a button or any view which should be visible in all the view controllers and its action can be called from all the view controllers? Popping and pushing of viewcontrollers should take place behind that button. I don't have any code but I have a reference app, Moise Bently, in this app it has a button on top right corner.
Try adding _yourView/button to your window... you can do this in didFinishLaunchingWithOptions also you can do [self.window bringSubviewToFront:_yourView] when required.. if you add some other view over it.
I have a super simple presentation for iPad with few modal views. At every View user makes a simple action (like dragging a slider), after that appears UIButton "Next" what presents new modal View above previous. Button in last view connects with first view by a modal segue. So app needs to work in recursive way. But after first time presentation goes (from the second loop starts), all Views presented with all actions done in them. So I want that all app ModalViewControllers reloads to the default state every time user taps button on the last View.
Can I implement this behaviour only set it in -prepareForSegue of UIButton in last ModalViewController or it's a more global issue? If so, how to implement that?
Implement viewWillAppear in every ViewController will give you an opportunity to reset the view before the view gets to the screen. When you come back to a ViewController from another viewController, viewWillAppear gets called.
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"viewWillAppear");
[super viewWillAppear:animated];
//Reset your view (i.e. hide next button etc.)
}
Hi there and thank you in advice for your help. I have a really strange problem while working with ViewControllers in Xcode4. First of all I have to say that I'm not using storyboards and I prefer to create any UI element programmatically. So I've set a UIButton and I want that, when pressed, it brings me to a new view controller. This is the code I'm using for a button:
-(void)settingsAndExportHandle:(UIButton *)buttonSender {
SettingsViewController* settingView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
settingView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:settingView animated:YES];
}
This buttons is initialized and allocated in the viewDidLoad method of the RootViewController. I want to switch to the other view controller (in this case SettingsViewController) when I press the button.
The strange thing is that when I press the button, the animation that flips the controllers goes well, but when it finishes I obtain the EXACT same things that I had on the RootViewControllers (same custom views, same buttons, same all!). The question is: what I'm missing?? I have to say that I use ARC (automatic reference counting) so I can't release or dealloc the views and buttons I've created on my RootViewController.
Any help will be appreciated. Thank you all!
Pushing and and modally presenting view controllers does not deallocate the view controller that presented them. It simply adds the additional view controller to the stack. You'll need to implement a callback method so that when the user hits the button to flip back to root view controller, your settings view controller lets the root view controller know what's about to happen so you can call a method you've written to reset the interface back to whatever state you need it at. You may also be able to use viewWillAppear: but that's a little messy.
However, according to the Apple Human Interface Guidelines, the user expects that when they push a view controller or modally present it, the view controller they were on will save state and be exactly the way they left it when they came back. It's disconcerting and annoying when state is not preserved while in a navigation controller context. It is especially annoying when it's modally presented.
Think about this - A user is in a hypothetical Mail app. They start typing out an email and set a font size and a color. They tap on the add attachment button, which brings up a modal view controller that allows them to select a picture. They select the picture and the modal view is dismissed, and in your implementation, the mail composing interface would have reset and the email content would be gone or at the very least the selected font size and color would be back to the default. That's not a good experience.