Action not being called when UIBarButtonItem tapped - iphone

Note: I've only tested this on the simulator.
I want to add the target-action in code instead of connecting it as an IBAction in Interface Builder.
theButton is on the navigation bar at the top left, created in IB.
Steps:
Declared theButton as an IBOutlet and connected it in IB.
Added this in viewDidLoad:
self.theButton.target = self;
self.theButton.action = #selector(theAction);
I'm testing theAction by this:
- (void)theAction {
NSLog(#"theAction called");
//do some other stuff
}
When I click on theButton in the simulator, nothing happens. I don't see the NSLog statement at all.
What am I missing?

Have you connected the action to the button in interface-builder?
If not you should declare the action in your .h file
-(IBAction)theAction;
Change the name of the action on your .m file
-(IBAction)theAction{
}
And finealy connect the action to the button in interface-builder.

Change your function as below:
-(IBAction)theAction{
NSLog(#"theAction called");
//do some other stuff
}
And if you called like "#selector(theAction:);"
then change function as below:
-(IBAction)theAction:(id)sender{
NSLog(#"theAction called");
//do some other stuff
}
Hope it will be helpful to you.
Let me know in case of any difficulty.

Figured it out. Had to do with my configuration. I'm using a tab bar controller that uses the same view controller for multiple tabs. Each view controller shows different filtered data for some tab items.
Therefore, in such a configuration, you must be sure to connect the IBOutlets for each view controller that's contained by a tab bar item. I only connected one, which is why it wasn't working for some of the tab items.

Related

File's Owner didn't received Action

the ping1 and how1 below method didn't called from UIButton
so i was double checking it in xib what i am wrong.
but i can't found the problem.
to solve this only i can do is looking the xib and code
is there any know-how available to solve it?
something like insert some code when UIButton send event
anyone advice me it will be thankful
bonhyoung.
#interface ViewController : UIViewController
-(IBAction)ping1:(id)sender;
-(IBAction)how1:(id)sender;
#end
#implementation ViewController
-(IBAction)ping1:(id)sender
{
// i put the break point in here
}
-(IBAction)how1:(id)sender
{
// i put the break point in here
}
// some other code...
#end
You should connect some IBOutlets and check in viewDidLoad that your connections are right (ie that your IBOutlets are not nil once the view is loaded).
First make sure that the file owner's class is set to ViewController. This is in the IB pane, click on the File's Owner and select the 3rd tab on the right panel. It should say ViewController and not UIViewController.
Second make sure your actions are setup. Right click on the button, and make sure the Touch Up Inside is wired to the correct method.

TabBar Application with a View that has a UIButton on the View to goto another View outside the TabBar

I'm new to iPhone development, and multiple views (xib or nib) are really confusing me. This is what i'm trying to achieve...
using TabBarControllerAppDelegate
Have 5 different TabBar Items and have created 5 different Views thru the TabBarController
The First View has a UIButton that is a Next button that needs to go to another view called View2.XIB.
I setup a new UIViewController which references the View2 and an IBAction for the switchPage, etc but not able to get it to do anything when clicking on the button.
All of My TabBar buttons work but not able to Navigate to anything outside of the Tabbar
Any help in this regard will be highly appreciated. Anyone have any examples
IBAction switchPageButtonPressed:(id)sender
{
[self.tabbarcontroller.tabBar setSelectedItem:[self.tabbarcontroller.tabBar.items objectAtIndex:1]];
here 1 means ur 2nd tabbar
}
It is difficult to find the problem without the code, but I will assume your action code for the switchPage button is incorrect. You should use code similar to the following:
- IBAction switchPageButtonPressed:(id)sender
{
ViewController2 *view2VC = [[ViewController2 alloc] initWithNibName:#"View2" bundle:nil];
[self presentModalViewController:nview2VC animated:YES];
[view2VC release];
}
If you are confident your method works, then you will want to verify that the action is hooked up correctly. The easiest way to do this is to place a breakpoint on the method and run the app in Debug. When you click the button, the debugger should break on your method, if it doesn't, you will need to check your connections in Interface Builder.

Code for Reset Button in iPhone

I have a button called Reset in my iPhone application. It is for resetting purposes. I called viewDidLoad() method for this. Is it right?
How to reset a page in iPhone?
how to write code for this? Any help would be appreciated.
You should not call -viewDidLoad in your own code. It gets called on view controllers (VCs) when their view has just finished loading.
To return a VC to its original state depends largely on the specifics of the situation, but you could probably either set its properties and whatnot back to their original values or you could alloc and init a new VC, remove the old VC's view from the view hierarchy and add the new VC's view.
Alternately, you could just implement a -resetToOriginalState method on your VC.
resetToOriginalState is not a inbuilt method, you have to write this method on your own and instead of calling viewDidLoad call this method 'resetToOriginalState:' when the respective button is clicked. If you are creating button programmatically you can set the target/action of that button like this:
resetBtn is the instance of the UIButton:
UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(x,y,w,h)];
[resetBtn addTarget:self action:#selector(resetToOriginalState:) forControlEvents:UIControlEventTouchUpInside];
- (void)resetToOriginalState:(id)sender {
//Do your stuff here
}
If your using Interface Builder connect the action method (resetToOriginalState) to the button.

Navigation based application with an add button

I created a new Navigation Based Application project.Then in MainWindow.xib I added a button to the navigationbar. I would like to push a new View onto the screen where I can enter information, which will be added as an object to the array of the UITableView.
But I don't know where to write the IBAction to link the button to (Appdelegate or the RootViewController)? Because as you see in the screenshot, it resides in MainWindow.xib because the RootViewController is merely a Table and doesn't contain the navigation. But in the document view of MainWindow.xib it is located under the RootViewController.
Do I have to create a new View Controller inside the XIB as well and create an IBOutlet for it?
I tried putting the code inside my AppDelegate and reference the button to the delegate but it doesn't work.
Please help. Thanks in advance.
See the screenshot here: http://i56.tinypic.com/5djbcm.png
When you ask yourself a question "where does this action belong" it's most probably a controller because controllers handle event flows in your app. Next question - "What controller is in charge when this action happens? What controller is most interested in this action?". Answer in your case is root table controller (RootViewController instance). Create an IBAction method in it which will push form controller (one you use to enter information) to navigation controller.
// somewhere in RootViewController.m
- (IBAction)addNewEntry {
NewEntryFormController *c = [[[NewEntryFormController alloc] init] autorelease];
// ...
[self.navigationController pushViewController:c animated:YES];
}

Access UI elements created in Interface Builder in the controller?

This is a very simple iPhone / Cocoa question. I have a button that transitions between two views. I set most of this up using interface builder. When I click the button and the second view is displayed, how do I programmatically change the text of the button (to say 'back', for instance)?
In your class declaration, declare a button object, and make sure it is specified as an IBOutlet:
IBOutlet UIButton* myButton;
Once you save this change, if you go back to Interface Builder, you should see this outlet when you right click on your File's Owner (assuming you have specified the File's Owner properly). Associate this outlet with the onscreen UIButton by right-click dragging.
Now the on screen object is associated with your in-code name.
Whereever you want to change the text on the UIButton, just say:
[myButton setTitle:#"Back - or whatever else you want it to say"
forState: UIControlStateNormal ];
// you can set different title text for each state
// of the button (selected, active, or normal)
Alternately, you can assign a unique tag to the control and use -[UIView viewWithTag:] to get a UIView pointer to the control. In most cases, outlets are the preferred mechanism, but tags are useful in things like table cells.
You'll need to set up an outlet for IB:
#interface MyViewController : UIViewController {
IBOutlet UIButton *myButton;
}
Save, bring up IB, set the file's owner to MyViewController, then create referencing outlets from both your UIView and UIButton to the appropriate points in the file's owner in the Connections Inspector.
Then in the implementation, you could do:
[myButton setTitle:#"Back" forState:UIControlStateNormal];
If you get lost with the connections, I'd recommend having a look at the lecture notes and video lectures at http://www.stanford.edu/class/cs193p/cgi-bin/index.php , which go into some detail about iPhone basics.
Hope that helps.
Most of the posts so far have focused on creating the button, and are very correct. The following answers the rest of the question:
When I click the button and the second view is displayed, how do I programmatically change the text of the button (to say 'back', for instance)?
The easiest way, if I understand your circumstance correctly, is to use the plug-n-play UINavigationBarController. First you want to push your second view controller onto the view stack:
// In firstViewController.m
self.navigationController = [[UIViewController alloc]initWithNibName:#"secondView" bundle:nil];
[self pushViewController:secondViewController animated:TRUE];
When your second view controller is shown, you should automatically get a back button on the left side of the nav-bar. If there is a need to change the text of that back button, you can simply refer to it like so:
// In secondViewController.m
-(void)ViewWillAppear
{
[self.navigationItem.backBarButtonItem setText:#"GoBack"];
}
There are also the left and rightBarButtonItem(s) which are handy for more complex navigation. Here is a less plug-n-play scenario:
// In secondViewController.h
-(IBAction)goBack; (this should appear as an action in your associated nib file)
// In secondViewController.m
-(void)viewDidLoad
{
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"GoBack" style:UIBarButtonItemStylePlain target:self action:#selector(goBack)];
}
-(IBAction)goBack
{
// logic to be done before going back
[self popViewControllerAnimated:secondViewController animated:TRUE];
}
You need to define and connect an "outlet". Take a look at the documentation, making note of the section on "Connections and Bindings".