I am newbie at iOS programming. I want to put notifications on UIbutton (for example displaying the click count on top of its corner), shortly it informs some counting on UIButton.
Is it possible? Please give some ideas or some short code for how can I do it.
in the header file add
{
IBOutlet UIButton *counterbutton;
}
-(IBAction)pushed:(id)sender;
in the .m file
int count;
-(IBAction)pushed:(id)sender
{
count = count + 1;
counterbutton.text = [NSString stringWithFormat:#"count is %i",count];
}
In the interface builder connect the button to the view controller for both the IBAction and IBOutlet.
Flex_Addicted share that link in my question i guess sharing in here more discernible. In that example shows exactly what i asked and it is really easy do just add h and m files in your project. How can I add a badge to a standard UIButton?
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.)
I want to do something like UITabBarController. UITabBarController has property viewControllers into which I can add instances of UIViewController * in IB. I'm trying to do the same think with my own VC:
#property (nonatomic, copy) IBOutletCollection(UIViewController) NSArray *viewControllers;
but this doesn't work. Is it even possible for us?
EDIT 1.
Ramshad posted the proper sample, but using XIB. I would like to achieve it using storyboards.
EDIT 2. - at the end of bounty worth...
I question vaderkvarn's post because in case of UITabBarController it works. Also as Ramshad posted, it is possible in NIBs. So far, dasblinkenlight's post is the most correct, but not answers the question. I'm holding this question opened because we shall find out if it's restricted for us or what is the way.
PS: Why these downvotes?
Although it does not look like you can connect UIViewControllers to IBOutletCollections (or there are too many restrictions placed on using them), there is a simple solution that works when you use storyboards. This code goes into your viewDidLoad method:
_viewControllers = [NSArray arrayWithObjects:
, [self.storyboard instantiateViewControllerWithIdentifier:#"Controller1"]
, [self.storyboard instantiateViewControllerWithIdentifier:#"Controller2"]
, [self.storyboard instantiateViewControllerWithIdentifier:#"Controller3"]
, nil];
The reason why the outlet collection solution does not work is that a View Controller is not an outlet. From the documentation:
An outlet is a property that is annotated with the symbol IBOutlet and
whose value you can set graphically in a nib file or a storyboard.
A view controller is not a graphical object.
If you want to use an IBOutletCollection, you should only use one view controller, and put the views in the collection.
But if you want one controller for every view, you need to go for a more programmatic approach. An array with view controllers might be a good start, but I couldn't say as I don't know what you want to do with them.
Edit:
To be more clear as you don't seem to catch my point:
No, it does not has to be a way. A Storyboard is not an API, it is a graphical tool for drawing scenes and segues. It is specially designed for things like Tab Bar based apps.
If you right click on your Storyboard file and choose open as -> Source Code, you will see that a Tab Bar Controller have special elements that other View Controllers do not have. To mess around with the XML in a Storyboard file is beyond me.
If you want to go with Nib files, use Ramshads answer.
If you want to get as close as possible with storyboards, go with dasblinkenlights answer.
But the answer to your question (as far as I know) is NO, there is no way to accomplish this with storyboards. If it were, it would have been documented, which it is not.
I have done your requirement using UISegmentedControl + IBOutletCollection + presentViewController.
You can use any control instead of UISegmentedControl as your wish.
I have Added 3 different UIViewControllers to Xib and labelled as ViewController1,2,3.
I also added 2 extra methods. One for presenting the corresponding view and another one for dismissing the earlier populated view.
I have attached the Xib settings screen-shot below.
You can use only one UIViewController instead of 3 and reuse it with some logic :)
The methods are below:
#property (strong, nonatomic) IBOutletCollection(UIViewController) NSArray *ViewCollection;
//dismissing the earlier populated view
- (IBAction)dismiss:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
//presenting the corresponding view.
- (IBAction)resetAction:(id)sender {
UISegmentedControl *mySegment = (UISegmentedControl *)sender;
int index = 0;
for (UIViewController *viewItem in _ViewCollection) {
if (index == mySegment.selectedSegmentIndex) {
[self presentViewController:viewItem animated:YES completion:nil];
}
index++;
}
}
You can download the my sample application from here
Xib Settings Screen-shot
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 ;)
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.
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