I created a ViewController and it has 6 UIButtons.Each one has different tasks . Its work fine.
When i am creating one button (sample)
IBOutlet UIButton *loginnextbtn;
#property(nonatomic,retain) UIButton *loginnextbtn;
#synthesize loginnextbtn;
[loginnextbtn release];
-(IBAction)LoginNextBtnPressed:(id)sender;
then i delete these all IBOutlet ,property,synthesize,release .
I only added
-(IBAction)LoginNextBtnPressed:(id)sender;
Its work fine.My doubt is why we need to iboutlet for button.And also one important think is I dont change button title or image at anytime.I just use these buttons only for button click event .For this case can i use IBAction only ? Is there any problem occurs in my project while its running ..plz help me..
sorry for my bad english..
hey dragon there wont be any problem if u just use the IBAction for binding ur method with the button... If you are not changing the title or color of the button at run time then you dont need to take it as iboutlet... Just bind the button you took with the action you want to set for that particular button's click event...
Happy coding
Related
I need several UIButtons to all connect to the same IBAction so the buttons can all do the same thing without having to copy and paste the code. Please tell me if there is a way to do this! It might be right under my nose, but I can't find it. Thanks!
Simply add the same target and selector to each button:
[button1 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
// etc.
For this You need to use set IBOutlet for Each Button or Set tag for each button if you are using Outlet then used this code .h
#interface RootViewController_Phone : UIViewController
{
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
- (IBAction)buttonPressed:(id)sender;
-(void)CallButtonsMethod;
#end
Now in .m file
- (IBAction)buttonPressed:(id)sender{
if([sender isEqual:btn1])
{
[self CallButtonsMethod];
}
if([sender isEqual:btn2])
{
[self CallButtonsMethod];
}
}
-(void)CallButtonsMethod
{
//your Code
}
Use IBOutletCollections.
Here is tutorial for that:
http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html
I've found that I'm usually not able to hook up multiple buttons (or even a single button, for that matter) in IB to an already existing IBAction in code, by Ctrl-dragging from the button to the IBAction in the m file. Xcode tries to create a new action in the dragged-to file, but won't connect to an existing action. [This is true for Xcode 4.6.1 and several previous (maybe all) versions.]
The approach in the accepted answer in the following link works if the IBAction is in the view controller which is the "File Owner" for the view containing the button:
Connect object in .xib to existing IBAction
However if you want your IBAction to be in the view's own class, rather than the view controller's class, then the following approach works:
Drag only one button onto the canvas in IB first. Control-drag from
the button to the view's .m file.
Drop the dragged line in any vacant space in the implementation section of the code.
Define a new IBAction in the little popup dialogue box. So now you have one
button hooked up to an IBAction.
Now instead of dragging more buttons from the object library, hold
down the Option key and drag out a new button from the original one.
This new button will come hooked up to the same IBActions as the
original one. (If you need to create lots and lots of buttons and
this seems tedious, you can select a bunch of already created ones simultaneously and
Option-drag to create as many more in a single go.)
its the annoying noob again!
So I was trying to develop a small app to check files on a folder.
The text field is your search field.
You have a picker view with 3 options
Folder A
Folder B
Search on Google.
I've got a question:
1: How do I connect the search field and the pick that the user chose to a button so when the user clicks the button, lets say the button searches the users input on google?
I've made the application for 2 views.
One where the user inputs the information and the other to display that information.
My code is going something like this..
- (IBAction)buttonSearch:(id)sender {
self.userSearch = self.searchField.text; ///Users input on the search field
NSString *searchString = self.userSearch;
///I know Im missing the connection to the button with the pickView, how could I do that too?////
///Here should be something like go to google(or whatever choice the user made on pickview) and put the string on the search bar and hit search/// <---How do I do this too..? *****
}
#end
Thanks for the help, I know its a pain to answer such stupid questions.... :/
Have a nice day!
If you're using interface builder to create your view make sure to create IBOutlets for the textfields/pickers/etc. so that you can get their values.
#property (nonatomic, strong) IBOutlet UITextfield *myTextField;
Now in interface builder right click your text field and select "NewReferencingOutlet" and drag to "File's Owner" you should see "myTextField" as an option. Once that is attached you can then get the value of the text field like this.
self.myTextField.text
It will be similar for the pickerView except instead of UITextField you will use UIDatePIcker or something similar...
Hope that helps ;)
I'm new to iPhone development, so I'm not sure if it is a very common issue. I didn't find anything about this on Google.
I have a UIBarButtonItem defined with Interface Builder:
#property (weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;
For now, it's just a simple button, but I want to put a camera image on it, using UIBarButtonSystemItemCamera. I know it's possible to do it with initWithBarButtonSystemItem, but I there is no method like changeWithBarButtonSystemItem. I'd like to something like:
- (void)viewDidLoad
{
[super viewDidLoad];
[cameraButton changeWithBarButtonSystemItem:UIBarButtonSystemItemCamera];
}
How is it possible ? Is there a way to tell Interface Builder to instantiate directly the button with this camera icon ?
Edit
It seems that it's not possible to change a button style after its instantiation. So only one question remains: Is there a way to tell Interface Builder to instantiate a button with UIBarButtonSystemItemCamera ?
In Interface Builder, change the UIBarButtonItem's "Identifier" to "Camera."
If you need to change buttons at run-time, the easiest thing is to swap out buttons themselves, not try to mutate them.
I used the tabview setup to create an app that has several tabs. One tab has a UIScrollView on it which loads 12 different views into 12 pages. I tried adding a button and imageview to one of the views and now it crashes. I've pored over every single question on here I can find, and have tried every suggested solution, but to no avail. The error that I get is the famous "this class is not key value coding-compliant for the key foo" error. As with everyone else who had this problem, if I disconnect the outlet, it works fine. I've checked and rechecked the class for the nib and it points to the right place.
So I'm not even sure where to go next.
I could post some code, but I don't even know what code would be helpful to post. I'm hoping someone just says, "Um yeah, you can't have a scroll view with multiple views in it and expect to have functionality on each page." That would be helpful!
Page2.h:
#import <UIKit/UIKit.h>
#interface Page2 : UIViewController {
IBOutlet UIImageView *infoImage;
}
#property (retain, nonatomic) UIImageView *infoImage;
(IBAction) showInfo:(id)sender;
#end
Page2.m:
- (IBAction)showInfo:(id)sender {
// do something please!
}
Then in the nib file I CTRL-dragged from "touch-up inside" on my button to the Files Owner and chose "showInfo" ... and I CTRL-dragged from Files Owner down to my UIImageView and chose infoImage.
CRASH
I am not cool enough to insert an image: http://i.stack.imgur.com/KjWhk.png
You still have an outlet connected in your nib that you no longer have defined in your class. You'll see in the inspector that it has turned gray. Disconnect it, and the error should be gone.
are u able to create any other outlet in the same Nib file, as i used to get the same crash, and it got fixed only when i duplicated the same nib, i had implemented almost all methods to solve it. If i am not wrong , this link can help you out https://discussions.apple.com/thread/2431110?start=0&tstart=0
I have sent you the email and attached your project. Hope its working at your end now.
Happy Coding!
I've recently been doing a lot of Objective-C programming, and just got back into doing more iPhone development. I've done a lot of programming using MVC in other languages/frameworks, but I just want to make sure I'm using MVC properly in my iPhone Development.
I created a new iPhone Utility Application, which creates two views: MainView and FlipsideView. Both have a controller (FlipsideViewController and MainViewController) and XIB file of their own.
What I've been doing is putting the IBOutlet UIControl myControl variables in my MainView.h or FlipsideView.h files and then tying the controls in Interface Builder to those variables. Then I put any IBAction SomeAction myAction methods in the MainViewController.h and FlipsideViewController.h files and tying the events to those methods in Interface Builder.
This seems to be conceptually correct, but seems to cause problems. Say I have a button that when clicked it changes a label's text. So the Controller doesn't have a clue of what the variable name of the label is in the OnTouchUp event handler for my button. So I make a #property for it. But since the MainViewController.view property isn't of type MyView, I get warnings or errors whenever I try to access those properties from the view controller.
I am doing this correctly? Is there a better way to do this? If this is correct, how do I properly work with those variables without getting warnings or errors?
Thanks
Here's some code showing what I'm doing:
MainView.h
#import ...
#interface MainView : UIView
{
IBOutlet UILabel* label;
IBOutlet UIButton* button;
}
#property UILabel* label;
#property UIButton* button;
#end
MainViewController.m
-(void) buttonTouchUp:(id) sender
{
self.view.label.text = #"The button was pressed!"; //This gives error because label is not in the view structure or union
[self.view label].text = #"The button was pressed!"; //This gives a warning
}
I know I can cast the view to be of the right type, but that seems like a hack job.
I know I can live with the warning, but a warning says to me that I'm doing something that I probably shouldn't be doing. I don't believe the SDK would set me up to do have to do something like that all the time.
I must just be misunderstanding what the View is supposed to be and I'm using it incorrectly.
Most code I've seen and written myself keeps all the outlets and the actions on the controller. The button sends a message to the controller (via IBAction), then the controller updates the label (via IBOutlet). In other words, putting outlets on your views is unusual.
Views shouldn't be connected to other views unless they have some special relationship, like maybe back/forward buttons that target a UIWebView... but even then, you find out you need a controller to enable/disable the buttons as appropriate.