Can we have data/custom attribute for buttons in ios? - ios

Hello there im basically from web development i have a requirement where i have to send a unique alpha numeric value from one view to another view on click of the button is their any way around we can set custom attribute or data attribute for buttons in Ios one like in htm5 ?

The easiest method is to subclass UIButton.
#interface MyButton : UIButton
#property (strong) NSString *myCustomData;
#end
#implementation MyButton
#end
You could also override the look of the button within this custom class to apply the same button style to all your buttons at once.

Related

Adding custom protocol to UITextField

I have some forms for the authentications and signup views and I want that all UITextField inside those forms have a UIButton as an accessory view, just above the keyboard. I want to have the possibility to set the title and the action for this button
Because I want all those text field have one and each will have a title and an action, and to avoid redundancy, I thought about a protocol.
I want something like extending a custom protocol, for example UITextFieldAccessoryViewDelegate and to be conform to some functions like :
-buttonAccessoryView title ... -> String
-didClickOnAccessoryViewButton.. -> ()
My mind is closed. Someone can give me some ideas to do what I want ?
You could use associated objects to solve this problem. This lets you add a property and its synthesized getter/setters.
#interface UITextField(AccessoryButton)
#property(readwrite, strong, nonatomic) UIButton *accessoryButton;
#end
#import<objc/runtime.h>
#implementation UITextField(AccessoryButton)
-(UIButton*) accessoryButton
{
objc_getAssociatedObject(self, #selector(accessoryButton));
}
-(void) setAccessoryButton:(UIButton *)accessoryButton
{
objc_setAssociatedObject(self, #selector(accessoryButton), accessoryButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
Include this category into your forms that need the UIButton for the text fields. Then assign action and title to a UIButton like how you normally do.
Considering everything you want to do I think you are better off using a subclass of UITextField rather than an extension.
Extensions can't add instance variables to the objects they extend, but subclasses can. As lead_the_zeppelin points out in his answer, you can use associated objects to simulate adding instance variables with an extension but it seems like you're making it needlessly complicated when a subclass gives you everything you want.

Show UIButton Created in Code in Interface Builder

In Xcode 6, can you have a UIButton property in your header file display in interface builder by setting the property as IBInspectable? The button is created entirely in code. Something like #property (nonatomic, retain) IBInspectable UIButton *directions;
I don't think uibutton are inspectable type. But why don't you just use a IBOutlet?
IBInspectable is just a way to design a set of none UI properties which can be then be accessible in IB to help you customize your class via the attribute inspector.
If you want to have a preview of your custom button, you should use IBDesignable, but in this case it doesn't apply to a property but a class.
IBDesignable allow to have a preview in IB of your view.
Check this article: http://www.weheartswift.com/make-awesome-ui-components-ios-8-using-swift-xcode-6/

How do I pass data from a text field in Flipsideview to Mainview? ios 7 xcode5

I'm currently writing an iPhone app that needs to pass data from a text field on the Flipside view to the Main view when the user clicks the Done button. This is the last component of a project that I am working on.
I've tried including my MainViewController.h file on my FlipsideViewController.m file, and this doesn't work. I'm currently writing in the - (IBAction)done:(id)sender portion, something like
MainController._aTextFieldOnTheMainView.text = _aTextFieldOnTheFlipsideView.text;
but I always get this error: Property '_aTextFieldOnTheMainView' not found on object of type 'MainViewController'.
What am I doing wrong?
Change/remove your weak declaration of #property (weak, nonatomic) IBOutlet UILabel *aTextFieldOnTheMainView;. This causes the deallocation of the property when the flipsideview comes onto the screen.
hope this helps

Dynamically show/hide a UIPickerView that was created using the interface builder

I'm trying to create an iPad/iPhone app that dynamically creates menus depending on a JSON object that is returned from a RESTful API. Specifically I'm trying to show/hide a UIPickerView that I created using the interface builder. In the properties menu in the interface builder I checked the box "hidden" for that UIPickerView. I used this tutorial to create to UIPickerView. I've set the delegate and data source to the View Controller using the interface builder. I'd like to unhide/show the UIPickerView when a certain condition is met. So far I've tried the following code:
[self.choicePicker setHidden:NO];
self.choicePicker.hidden = NO;
I usually build such object programmatically but I thought I'd try it this way. I've looking through various stackoverflow posts and doing research but I can't seem to find something that works. I'm new to programming in Objective C. Thanks in advance any help is greatly appreciated.
.h file code
#interface slrpViewController : UIViewController<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
{
IBOutlet UIPickerView *picker_choice;
NSMutableArray *dataArray;
NSMutableData *receivedData;
}
#property(nonatomic, strong) UILabel *nameLabel;
#property(nonatomic, retain) UIPickerView *choicePicker;
.m file code
-(void)buildChoicesMenu:(NSDictionary *)choiceDict{
//in this method we build the choices menu
[self.choicePicker setHidden:NO];
self.choicePicker.hidden = NO;
if (self.choicePicker) self.choicePicker.hidden = !self.choicePicker.hidden;
}
You have two different picker views defined. One a property choicePicker (and an implicit _choicePicker instance variable), and another instance variable picker_choice. It seems you have connected your picker_choice in interface builder, but are trying to modify the property. In fact, if you try to print po self.choicePicker in the debugger, you would get nil, because there is nothing filling it.
Either remove the instance variable, and connect your property in interface builder, or synthesize your property with your instance variable by doing so:
#syntesize choicePicker=picker_choice

Many UIButton in .xib with different tags

I have 17 button in my xib. And I have set them tag values 1 to 17.
Can somebody can tell me, how to connect all 17 buttons with a single variable name in .h and get a particular button in .m with it's tag value.
Thanks
I just tested this, and I know that if you select all your buttons in storyboards, and control drag them to the appropriate controller in an assistant editor you can create a collection of outlets representing all the buttons. The resulting code was:
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
no its not possible as per my experience. IBOutlet i.e. Interface Builder Outlet always refers to a single connection between an interface component (like button) and a variable in interface (like IBOutlet UIButton *myButton). This one-to-one relation.
There needs to be an IBOutlet per button, so you'll need to create all 17 of them. Connect buttons to outlets individually, and then you will be able to put them into an array inside your initializer if you need them in an array.
You can save on the IBAction methods, though: make one method like this
-(IBAction)buttonClicked:(id)sender {
}
You can connect this method to all buttons, and look at the tag of the (id)sender to decide which button called your action.
You can't give a one reference to 17 button but you can assign one method to 17 buttons like #dasblinkenlight said
You have to just set the IBAction method to all button clicked event
and using tag value you can access the button which you want
-(IBAction)buttonClicked:(id)sender {
int j = [sender tag];
NSLog(#"Clicked Button %i", j);
}

Resources