how to raise modal view from tab bar button - ios

I'm using this:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
Anyone know how to get a modal view raised from say..the camera button (if the camera button were to be used for something else other than the camera)?

In BaseViewController.m the centre button is added in
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
Simply add the observer:
[button addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
And then obviously implement:
-(void)click:(id)sender{
[self presentModalViewController:[[UIViewController alloc] init] animated:YES];
}

Related

viewDidDisappear called because of termination? [duplicate]

When a view loads, i want to see if it's because the user pressed the back button. How can i check this?
The best solution I've found to detect a UINavigationController's back button press (pre-iOS 5.0) is by verifying that the current view controller is not present in the in the navigation controller's view controller stack.
It is possibly safer to check this condition in - (void)viewDidDisappear:(BOOL)animated as logically, by the time that method is called it would be extremely likely that the view controller was removed from the stack.
Pre-iOS 5.0:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (![[self.navigationController viewControllers] containsObject:self]) {
// We were removed from the navigation controller's view controller stack
// thus, we can infer that the back button was pressed
}
}
iOS 5.0+ you can use -didMoveToParentViewController:
- (void)didMoveToParentViewController:(UIViewController *)parent
{
// parent is nil if this view controller was removed
}
in your viewWillDisappear method check
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self isMovingFromParentViewController]) {
//specific stuff for being popped off stack
}
}
This is only for post iOS 5
UINavigationController has a delegate property that issues delegate callbacks. Please see the iOS reference here.
The delegate doesn't have a "back button pressed" callback, but instead it tells you when something is going to appear on the navigation stack. When you press back, you are "popping" the top view controller off the stack, so it will tell you that the view is about to appear. I think this is the callback you'd be looking for.
You could have some simple logic to check if it's the view controller that's "interested", and then you could send a notification, et al.
For the sake of completeness, mix of two most upvoted answers (1, 2) in Swift:
override func willMoveToParentViewController(parent: UIViewController?) {
super.willMoveToParentViewController(parent)
if parent == nil {
// view controller is popping
}
}
This is a slightly different scenario, but I thought the solution might help others out.
In my situation, I had a UINavigationController within a UIPopoverController. I needed to detect whether the user clicked the back button, or clicked outside of the popover. To do this I checked the visibleViewController property in viewWillDisappear. If the view controller is still the visibleViewController when closing, then the popover is being closed by another means. If the view controller is not the visibleViewController when closing, then the back button was pressed.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (self.navigationController.visibleViewController != self) {
<Do something since we're closing using something else>
} else {
<Do something since we're closing because of the back button>
}
}
I tried using zach's solution, but isMovingFromParentViewController returns true for both cases.
I verified this works in iOS 5+
I hope this helps.
Create a custom back bar button and set the target,
Step 1: Add these methods to your class
- (void)backButtonClicked :(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)addBackBarButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 55, 35);
[button setTitle:#"back" forState:UIControlStateNormal];
[button addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
Step 2: Call [self addBackBarButton]; in viewDiDLoad method
You will get the action in backButtonClicked method. You can play around with it the way you want.
Cheers!
The only way to do this so you know for sure that it was the back button is to create a custom button. If you don't know how to do that, check out this tutorial. It won't look exactly like the normal back button, but close. If you need more help, post a comment

Use another button to do the same thing as the back button

What I have now?
I have two view controllers (VCA & VCB) and a navigation controller. VCA can push to VCB and VCB can go back to VCA by pressing back button in the navigation bar of VCB.
What I want to do?
I want to add another button in VCB to do the same thing as the back button in navigation bar does, that is when that button is pressed, I should be lead back to VCA, how can I do it?
What I have tried?
I tried to add a button in VCB and controller drag a line from the button to VCA so that they are connected, but I found that VCA's viewDidLoad method is called each time I back from VCB, this is not what I wanted.
Thanks!
You can realize some button (object of UIButton class), for example like this:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton.frame = CGRectMake(60.0f, 40.0f, 200.0f, 50.0f);
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:28.0f];
backButton.titleLabel.shadowOffset = CGSizeMake (0.0f, 1.0f);
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[backButton setTitleShadowColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
And also you must to define corresponding selector for action of the button:
- (void)goBack {
[self.navigationController popViewControllerAnimated:YES];
}
Now, if you click this button you will come to previous view controller.
The action that is fired when you press a back button to pop a ViewController is:
popViewControllerAnimated:(BOOL)animated
So what you can do it to create a button and add that action to it:
UIButton *goBackButton = [[UIButton alloc] initWithFrame:CGRectMake(0,64,50,50)];
[goBackButton addTarget:self action:#selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goBackButton];
Now you actually have to implement that goBackAction:
-(IBAction)goBackAction:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
You could actually add the popViewControllerAnimated action to the button, like:
[goBackButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
The problem is that you are not able to pass that animated parameter to the method which will result in it using some junk that happens to be where that parameter was expected to be in memory. The result will be the pop sometimes being animated and other times not.

Ios How to a add a button to my TabBarController then trigger the presentation of a modal view from it

I have a UITabBarController bassed app. I'm instantiating it from the app delegate and adding a custom button in the tab bar controller. when that button is clicked, I want to present another view modally, but I cant seem to figure out how to do it. to add the button I'm basically doing this
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.tabBarController.view addSubview:button];
[button addTarget:self action:#selector(showModalViewController:) forControlEvents:UIControlEventTouchUpInside];
and also in the app delegate I have a method
- (void) showModalViewController {
DummyViewController *addController = [[DummyViewController alloc]
initWithNibName:#"DummyViewController" bundle:nil];
//addController.delegate = self;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
// Create the navigation controller and present it modally.
[self.tabBarController.selectedViewController presentModalViewController:addController animated:YES];
// The navigation controller is now owned by the current view controller
}
I keep getting unrecognized seletor
Your selector looks for a method named showModalViewController: but your actual method is named showModalViewController. Change one or the other.
Either change the selector to #selector(showModalViewController) to match the existing method, or change the method to:
- (void)showModalViewController:(UIButton *)button {
Don't change both.

cannot add right bar button item to navigation item

I would like to implement a method to add a done bottom to the right of the title navigation bar as so as some one starts entering text in a UITextView but for some reason nothing happens. The method gets called but does not add the button:
-(void)textViewDidBeginEditing:(UITextView *)textView{
UIBarButtonItem *doneButton;
[doneButton setTarget:self];
[doneButton setStyle:UIBarButtonItemStyleDone];
[doneButton setAction:#selector(keyboardDone:)];
[self.navigationItem setRightBarButtonItem:doneButton animated:YES];
}
Am I doing something really basic wrong here?
You need to alloc and init a UIBarButton item before it's ready for use. All you did was declare that "doneButton" was a UIBarButtonItem but you never properly set it up for use.

send a message to a parent object form popover

my iOS application use storyboard
it have 2 view controllers:
- main storyboard view controller
- and popover view controller with some objects in it
i've got a button on main view controller and it creates programing every time i run the application:
*CGRect buttonFrame = CGRectMake(10., 10., 120., 50.);
oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[oneButton setImage:[UIImage imageNamed:[NSString stringWithFormat:#"someImage.png", img]] forState:UIControlStateNormal];
[oneButton setTag:img];
[oneButton setFrame:buttonFrame];
[oneButton addTarget:self action:#selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:oneButton];*
the action of this button show my popover view like that:
*- (void) pressButton:(id)sender {
popoverViewController *popoverFrame = [self.storyboard instantiateViewControllerWithIdentifier:#"myPopoverView"];
popoverWithObjects = [[UIPopoverController alloc] initWithContentViewController:popoverFrame];
[popoverWithObjects presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}*
from now the situation is, that i can't send to my button any message or result.
i want to say to my program button (note - i've got only sender of this button action:#selector(pressButton:) ) that popover return some result or some action of an object in popover send anything (string for example)
Or in another words when i interact with any object like button on popover view, i want to change parent buttons title label
Use NSNotificationCenter.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
You need to use delegate. Check out my answer to this similar question from this SO.
Edit: Link to tutorial on Storyboard and delegate pattern usage. And my original answer to delegate on this SO
How about writing a function on your Main View Controller to do what you want. Then call that function from the popover? (i.e. use 'prepare for segue' to send the popover the id of the Main View Controller, and then use that id to call the function on Main View from the popover)

Resources