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)
Related
The issue I am having is that I want to call a method in a navigationController by clicking a button inside a UIView (footer). When I press the button, it should call the method I'm trying to access to open the Video recorder in the code below.
I was told I could implement a delegate method or use a NSNotification. Below is what I have:
My footer (ESPhotoDetailsFooterView.m) has my button that I created. My footer only contains a UIView.
The method I'm trying to access in my footer resides in (ESTabBarController.m)
This is what I am trying to trigger when pressing my button:
RecorderViewController *viewController = [[RecorderViewController alloc] init];
[viewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self.navController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self.navController pushViewController:viewController animated:NO];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:self.navController animated:YES completion:nil];
});
I am new to Objective C and understand the basics. I cannot figure out what I need to do to accomplish this. Any help would be much appreciated.
The code for the button is as follows:
// Create a standard UIButton programmatically using convenience method
UIButton *camButton = [UIButton buttonWithType:UIButtonTypeCustom];
// Set the location (x,y) and size (width,height) of the button
camButton.frame = CGRectMake(9.0f, 8.0f, 35.0f, 35.0f);
// Create UIImages from image resources in your application bundle
// using convenience methods (no need to release)
UIImage *normal = [UIImage imageNamed:#"BingComm"];
UIImage *highlighted = [UIImage imageNamed:#"BingCommClick"];
// Set the button's background to an image
[camButton setBackgroundImage:normal forState:UIControlStateNormal];
[camButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];
// Add the target-action for the touch event
#pragma GCC diagnostic ignored "-Wundeclared-selector"
[camButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.mainView addSubview:camButton];
Yes, in this case delegation is a good choice, basically you will need a delegate object in your footer view. Then you set the delegate to TabBarController at runtime.
When user clicks the button, call you delegate with your method [delegate method]
This is a very important design pattern objective-c, it would be very helpful if you can follow this tutorial to fully understand delegation.
http://www.alexefish.com/post/522641eb31fa2a0015000002
I agree that delegation is important and very much worth learning, but another way to solve the same problem would be to the following:
In your method definition for btnClicked, call the following code
if([self tabBarController]) {
[[self tabBarController] methodToCall];
}
Since your view controller should be embedded within a tab bar controller, it will have this property set. You can then call any public methods contained within the tabBarController class.
I have created a button in a UIView subclass . I need to call popViewControllerAnimated via this button , but nothing work ! and I cannot see the viewController push back to rootViewController . here is my code :
- (void)SomeFunction {
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton showsTouchWhenHighlighted];
[backButton addTarget:self
action:#selector(backToMainMenu)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)backToMainMenu {
[self.window.rootViewController.navigationController popViewControllerAnimated:YES];
NSLog(#"back");
}
I change the code to this :
UINavigationController *vc = self.window.rootViewController.navigationController;
[vc.navigationController popViewControllerAnimated:YES];
but nothing happens .
I think you need to use the proper target format which takes the button as an argument. So add target function like this:
[backButton addTarget:self
action:#selector(backToMainMenu:)
forControlEvents:UIControlEventTouchUpInside];
and the target should look like this:
- (void) backToMainMenu:(UIButton *) sender{
[self.navigationController popViewControllerAnimated:YES];
}
A better option is to use Delegate pattern because in your current logic you are breaking the MVC architecture and guidelines.
Create a Protocol in your subview class. The receiver of this delegate would be the view controller class from which you are showing your view. In event handling of the button, call the delegate method and from the view controller you would be able to call popViewControllerAnimated successfully.
I believe your fundamental problem (besides design) is in (void)backToMainMenu ... self.window.rootViewController.navigationController will be nil, so this method does nothing
from UIViewController class reference:
If the receiver or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.
so you see, the rootViewController cannot be embedded inside a nav controller can it, it is the bottommost one..
why don't you test this:
{
UINavigationController *vc = self.window.rootViewController.navigationController;
if (vc==nil){
NSLog(#"nav controller is nil, this will never work");
}
[vc.navigationController popViewControllerAnimated:YES];
}
I also fully agree with #rory's answer there about design..
PS did you actually create a UINavigationController in order to push this viewController?
If I want to extend a UIButton for example and have it present a new view. Can I create a method within my custom UIButton class to do this. If I initialize the button with a target method that ends up switching views it needs to know what view it is currently in.
ExpandedViewController *expandView = [self.storyboard instantiateViewControllerWithIdentifier:#"ExpandedView"];
[self presentViewController:expandView animated:NO completion:nil];
This is what the code would look like in a view controller, but how can I pass the view to the button so that instead of self, it knows what view to target.
The short answer is that yes, this is possible.
The long answer is that you shouldn't do it because it breaks the rules of MVC. Your button is a view and should not be acting as a controller (this is what the controllers are for). Instead, leave the logic to your view controllers.
To get the correct view controller set as the target of your button, you can do this:
In viewDidDisappear: of your view controllers, remove the action for the controller that is going away from the button:
[myButton removeTarget:self
action:#selector(myButtonWasPressed:)
forControlEvents:UIControlEventTouchUpInside];
And then in viewWillAppear: of your view controllers, add the action for the controller that is being presented:
[myButton addTarget:self
action:#selector(myButtonWasPressed:)
forControlEvents:UIControlEventTouchUpInside];
Give your button a containingViewController property and don't forget to set it as you move the button around.
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.
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];
}