iOS dynamic addition of UIControls - objective-c

I'm trying to add a bunch of UIButtons to a screen at runtime. Based on user input I want to show different buttons in different locations. Is this possible?

Yes, it's possible.
Creating a button in objective-C:
UIButton *btnPrefix = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPrefix setFrame:CGRectMake(0, 0, 20, 20)];
[btnPrefix setUserInteractionEnabled: YES];
[btnPrefix addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: btnPrefix];

Related

iOS 7 Universal Back Button Image

This has been covered disparately in other questions and answers but this is a more general request for advice.
I am writing a scientific App so the titles in the Navigation Bar have a habit of being long. So I need to contrict the size of the back image or text.
If I do this at the View Controller level it works perfectly but it means inserting the code in every VC. E.g.
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 36.0f)];
UIImage *backImage = [[UIImage imageNamed:#"backButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
As I want this to be consistent what is the best approach in iOS 7 to put it in the App Delegate so it is only instigated once?
The best approach in my opinion would not be to put this in the App Delegate, It would be to create a subclass of uiviewcontroller and add it there in the viewwillappear than have all the view controllers in your app of "that" kind.

Glossy effect when clicking on UINavigationBar button

When I'm clicking on a UIButton that located on the top of UINavigationBar I have this glossy effect:
How can I add this effect to another button that located on a UINavigatonBar but not as a left or right button item? like this:
UIButton * emptyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[emptyButton setFrame:CGRectMake(119, 5, 82, 35)];
[emptyButton addTarget:self action:#selector(centerNavigationBarTitleClicked:) forControlEvents:UIControlEventTouchUpInside];
[emptyButton setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:emptyButton];
Thanks!
You simply have to set the showsTouchWhenHighlighted property to YES to get the glow effect, i.e.
emptyButton.showsTouchWhenHighlighted = YES;

How to create a action to a button in Cocoa programmatically?

I'm trying to create a button in Cocoa in mac programmatically, but I don't know how to put a action on this, I'm trying like this:
NSRect frame = NSMakeRect(10, 200, 80, 100);
NSButton *btn = [[NSButton alloc]initWithFrame:frame];
[btn setButtonType:NSMomentaryPushInButton];
[btn setBezelStyle:NSRoundedBezelStyle];
[btn setTitle:#"Click me"];
[btn setAction:#selector(hideLabels:)];
[view addSubview:btn];
but the line [btn setAction:#selector(hideLabels:)]; don't work, how can I create a action here?
the method hideLabels is in function, because I used it with another button.
Does your hideLabels method take an argument?
if not, leave off the : from the selector

Can you hard code IBActions and IBOutlets, rather than drag them manually in Interface Builder?

Is it possible to to hard code the IBActions and IBOutlets in code, instead of drag-connecting them in Interface Builder?
Yes it is poosible...
sample code
UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
[btnDetail setFrame:CGRectMake(250.0f, 15.0f, 65.0f, 20.0f)] ;
//btnDetail.backgroundColor = [UIColor grayColor];
[btnDetail setTitle:#"Detail" forState:UIControlStateNormal];
[btnDetail setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[btnDetail.titleLabel setFont:[UIFont fontWithName:#"Verdana" size:12]];
[btnDetail setBackgroundColor:[UIColor clearColor]];
[btnDetail sizeThatFits:btnDetail.frame.size];
[self.view addSubview:btnDetail];
//IBAction
[btnDetail addTarget:self
action:#selector(ShowSavingAccountDetail:)
forControlEvents:UIControlEventTouchUpInside];
[btnDetail setImage:[UIImage imageNamed:#"btn-detail.png"] forState:UIControlStateNormal];
The concept and sole purpose of IBAction and IBOutlet is to provide Interface Builder with means to connect the xib with your code.
If you don't want to use Interface Builder with your code, you don't need IBAction or IBOutlet, you need them ONLY to use objects (buttons, textfields, etc.) that were instanciated in your xib from your classes.
With that said, mihirpmehta's answer is the correct way to programmatically add UI elements to your view and add actions to them.

UIButton like in UIActionSheet

I would like put UIButton on my UIViewController with style like have buttons in UIActionSheet. What should i add here:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[ button setFrame: CGRectMake(10, 10, 50, 35) ];
[button setTitle:#"Button from UIActionSheet" forState: UIControlStateNormal];
[self addSubview: button];
You need to get the background image from somewhere and then set it using: - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
I believe the .psd linked here has the action button background image http://media.photobucket.com/image/uiactionsheet%20button%20psd/visionwidget/iphone-gui-psd-file.jpg
you can using segmented control - very good trick
segmented = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Info",#"Configuration", nil]] autorelease];
[segmented addTarget:self action:#selector(changeView:) forControlEvents:UIControlEventValueChanged];
segmented.tintColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.52 alpha:1.0];
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
Maybe exists some simple way like set some property?
Here is a post on using Apple's private UIGlassButton class.
You can't submit an app like this to the app store, but you can capture the generated background image to reuse in your published apps.