Change property of a button programmatically - iphone

I am working on another person's code. He has created some UIButtons in a UIView in the interface builder(not programmatically).
But now the UI has changed slightly. When the view loads, based on some conditions, some buttons have to be disabled/enabled. I am a bit confused about, whether to alter the code by creating buttons dynamically and disable the buttons based on conditions, or is there any other way like referring the button by tag and changing its property. Sounds like a silly question, I am still working on improving my basic skills in Iphone app development.

Create an IBOutlet to the UIButton, then you can do things like:
myButton.enabled = NO; // or YES
myButton.hidden = NO; // or YES

You would need to create an IBOutlet using interface building which will give you a reference to the button in your nib file. From there you can modify the parameters on the button which was loaded.

Related

Custom Look And Feel of a text field in iOS

I'm a beginner with Objective-C and have a question regarding creating a custom look and feel text box. I want to be able to create a text box area that has, for example a header with a label and different background color etc. where the text input appears. How does one go about doing such a thing in Objective-C? Is it a matter of just creating a custom class that has a UILabel with specific coordinates on top of the UITextBox? Let me know if someone can point me in the right direction. Thanks.
One option is to create a custom view that contains a UITextField and UILabel as sub-views. After that, the view can be re-used.
I highly recommend reading the View Programming Guide # Apple: http://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/ViewPG_iPhoneOS/Introduction/Introduction.html
A related post on creating an aggregate custom view: .Net UserControl XCode equivalent
Here's one tutorial on creating a custom view:
http://www.raywenderlich.com/1768/how-to-make-a-custom-uiview-a-5-star-rating-view
You can also look at how other custom controls are built. Many of the controls at cocoacontrols.com are open sourced: http://www.cocoacontrols.com/
Use layer property of text field like
#property (nonatomic,weak)IBOutlet UITextfield *email;
so in implementation file or view did load method
self.email.layer.backgroundColor = [uicolor orangeColor];

Updatable, custom view on a UIToolbar

I want to make a small area to present some information in the middle of a UIToolbar and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView: to create a UIBarButtonItem, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
Using initWithCustomView: sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)
You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.

Link a UILabel and a UIButton in IB

During a time in my app - I make a button a button not enabled
myButton.enabled = NO;
The problem is that I've made my button in IB with an image, and just a UILabel overtop of it.
The label does not grey out when the buttons does.
In IB - is there a way to link the label to the button?
This is not possible without you doing the linking action yourself, as #Eiko rightly pointed out.
It sounds like you need to make your image the background-image of the button, so you can have your label as the button text, like it is intended to be used. Then you can specify colors, fonts & images for all 4 possible states.
If you decide to invent the wheel yourself, by keeping button and label as separate objects, you will have to invent everything around it as well.
You can add another outlet in your code, i.e. IBOutlet UILabel *yourLabel;
Then link this outlet to your label, same procedure as linking the button.

iPhone keyboard doesn't appear when entering a UITextField

This has to be some kind of newbie blunder that I just can’t see, and I’d be grateful for hints as to what to check or where to look.
I've followed an iPhone tutorial that has a UITextField, making sure I connected the IBOutlet for the text field, and it seems to compile properly (no errors or warnings). But when I run it under the simulator, and click in the field, I don’t get the keyboard, so I can’t enter anything into the field.
I’ve tried searching the site for similar questions, and all I’ve found is a few questions where the developer is trying to set up some complex UI with multiple controllers, and one that seemed to be the same issue, but the original poster simply said that he solved it by starting a new project and porting the code over. I’d like to find an actual solution, so I don’t have to try randomly rebuilding projects when this issue comes up again.
Thanks!
Just to cover all your bases when troubleshooting this, make sure it's not the iOS Simulator that's causing this. I wasn't seeing the keyboard appear when entering a UITextView as well and it turns out the simulator allows you to toggle between displaying the virtual keyboard and just having you use your laptop's keyboard.
You toggle between the two via:
Hardware > Keyboard > Toggle Software Keyboard
or press Cmd + K.
(my source: https://stackoverflow.com/a/24401535/1700540)
So make sure you didn't mistakenly toggle the simulator to only use your physical keyboard! ... like I had
Make sure parent containers have User Interaction Enabled checked. It is not sufficient for the individual controls to have this checked. The UIView can inadvertently get this deselected.
One thing you should check is to make sure the containing view (the UIView that contains all the controls), which is the View icon in Interface Builder nib viewer, has User Interaction Enabled checked. If the parent container doesn't have this checked, even if the individual controls do, you will see this behavior.
What is the delegate for the UITextField connected to? Have you made sure that the delegate functions, particularly - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
is returning the right thing (in this case YES)?
Not sure if this is anyone else's problem but I was using a TextField in a TabBar controller displayed modally and I added the shake gesture in my main view controller. This unfortunately meant I was setting [self becomeFirstResponder] in the viewDidLoad. In that case when my modal view displayed it was not becoming the first responder and hence the keyboard did not display.
To fix it I added [self resignFirstResponder] just prior to calling the modal display and all is well once more! Yay (only took five days to figure that out).
Basicly the only thing you need to do is declare the text field in the .h
IBOutlet UITextField *textField
then declare with same name in the property
#property (nonatomic, retain) UITextField *textField;
then make sure you synthesize in the .m
#synthesize textField;
then you have to link the field in the interface builder

Floating a UILabel above OpenFlow

How do you get a UILabel to float above Alex Fajkowski's implementation of CoverFlow called OpenFlow?
Ok I've figured it out. I just had to use bringSubviewToFront with the UILabel.
Thanks to everybody who answered.
Make an OpenFlow instance and a UILabel instance subviews of a parent view, placing the label atop with flow view using -insertSubview:atIndex:, -addSubview: or similar e.g.:
[self.view addSubview:myOpenFlow];
[self.view addSubview:myLabel];
To answer the second question, try this: edit the AFOpenFlowView in IB and add the label view as a subview of that wherever you want it to appear, then set the attributes on the view to Hidden if you don't want it to appear by default. Create an IBOutlet for the label view in your controller so you can easily manipulate it, such as setting hidden to show in order to show it. I'm not familiar with open flow so I'm not sure if it will programmatically create a subview that will obscure your label. If so you may need to use the outlet to move it to the front using UIView methods.
The Openflow example code shows a perfect demonstration of this. Alex adds an info button in the top right of the openflow view using interface builder. This is exactly what you are asking for.
I would add that if you can't understand what is going on from the example code, you should look at more code examples from Apple demonstrating simple UIView usage.
It's not so easy because the label name has to change for each picture. We have to work with index, and with event.. move.. tap.. selected...I'm working on too !