Using one IBOutlet with different objects - iphone

I was wondering how is it possible that use ONE IBOutlet to several objects , for example I have IBOutlet UITextView *myText; then 3 UItextView on one view , so I want connect all of them with myText !

You can have one NSArray which you declare as an IBOutletCollection (instead of IBOutlet). In order to know what text field you are getting out of the array, you can set a tag on each one in IB and just pull from the array the text fields that match specific tags.

Don't think so. Each UITextView in the one view would be a separate instance of the UITextView class. The myText variable would only be able to point to one instance's memory address at a time.
I'm not sure what you're attempting to accomplish but you might be able to 'fake it' by having all the UITextViews use the controller as their delegate and setting their properties equal to each other when ever changes are detected. (i.e. Every time the controller receives a message about the text property changing it sets all the UITextView objects text properties equal to the changed UITextView's text property).

You can use 1 object and have different tags.

This is inappropriate. However, it's appropriate to hook one IBAction with multiple IBOutlets. So that when different buttons are pressed, they go to the same IBAction. And the IBAction can tell where on earth that triggering is coming from by looking at the (id)sender argument.

Related

On iOS, how to make a dynamic list of "links" or buttons call a method with a string argument?

It seems that a common way to make a text behave like a link on iOS is to make it a UIButton, but I noticed the UIButton's addTarget or gestureRecognizer's addTarget both don't have an argument that can be passed to the method when the button is pressed?
The situation is, from a server, we may get back a list of words, such as "pineapple", "apple", "orange", and the numbers can vary. These words are displayed on screen, and pressing the word will invoke a ViewController to replace the main view controller.
It seems that one way is to use UIButton's tag, so when we set up the button, we give it a tag of 0, and in another array of the current view or view controller, make element 0 point to an NSString object, containing the word "pineapple". And so in the handler, the tag can be obtained, and it can be used to retrieve the string. But is this the only way, because it seems not very structural. Is there a better way?
My knee-jerk answer was to simply suggest that you subclass the UIButton. Whenever you want to add a property to an existing class, "subclass" is the first answer that comes to mind. But when I tried subclassing the UIButton, it did not work well. Searching for for "UIButton subclass" I discover that this is a well known issue with several recommended solutions:
Subclass UIButton to add a property
objective C: Buttons created from subclass of UIButton class not working
create uibutton subclass
I've tried both creating a category with associative references as well as the simplistic approach of just subclassing a UIView instead, and make the desired button a subview of that. Both approaches work fine. But the intuitively attractive option of just subclassing UIButton does not work well.
But while the various work-arounds for adding properties to UIButton objects work, they seem sufficiently unintuitive that, I'd be inclined to go back to something simple, such as using the tag numbers in an array or dictionary, rather than pursuing these cumbersome button subclassing techniques.
Create a mutable dictionary. To associate a word with a button, add the buttons address wrapped in a nsnumber as the key and the word as the object. When the button is pressed and sends the action message along with 'sender', you can retrieve the current word from the dictionary.

Accessing ParentViewController from UITextFields inside a custom View

I have a UIScrollView to which I append multiple custom UIView's that contain UITextFields...
In each of these text fields I handle the EditingChanged event so that I do some calculations and append them to a UILabel...
Now... Outside my UIScrollView I have a TextBox who's value I need to access from the TextFields inside my custom View...
Can I scroll up the hierarchy and find that control by it's tag somehow? I've tried using ParentViewController but that's always null.
Any help appreciated
You should be able to use the .superview property to walk up the view hierarchy to the top level UIView
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/superview
Is the TextBox outside the UIScrollView in the same UIViewController? You should be using a controller to manage the communication. Handle the EditingChanged in the lowest level that contains both text boxes. So, if they are in the same UIViewController, have it handle the EditingChanged and have a reference to the text box as well.
Or you can use UIApplicationDelegate.
Declare an instance referring to UISCrollView, and set UIScrollView to the instance.
#interface YourAppDelegate {
YourUIScrollView* yourScrollView;
}
#property (nonatomic, retain) YourUIScrollView* yourScrollView;
#end
Wherever you want to access the scrollview, you can do as below,
YouUIScrollView* yourScrollView = ((YourAppDelegate*)[[UIApplication sharedapplication] delegate]).yourScrollView;
You could go the other way and go down the hierarchy to get the value from the lower UITextField and perform the calculation outside of the UIScrollView. A way you could access the lower UITextField's is as follows:
foreach (scrollView.ViewWithTag(tagNum).SubViews.OfType<UITextField>())
{
// get the value from the textfield then perform the required calculation
}
(this of course assumes that you've given the the UIView's you're adding a Tag.

can i add an ivar (or property) to an existing class in objective-c?

I'm wondering if it's possible to add an ivar to the UIButton class? A bit like categories but not for a method(s) but for ivars.
I am programmatically creating and displaying an array of UIButton's which I then all link up to a single action method using –addTarget:action:forControlEvents: for a touchup event.
Now, my receiver method needs to know which of all the buttons was pressed but using the "(id)sender" approach doesn't cut it because the only thing differentiating all the buttons is the image its displaying and there is no way to get to that (I need a string). The buttons are all in different places so I could do some math to convert the position data into an "id" but if I change the positioning of the buttons down the line, I will need to change the math as well and I don't like that.
Can I just subclass UIButton and change nothing except for adding a (NSUInteger)idCode property? Then when I create the buttons I set the idCode, and when the target-action mechanism fires the action method, I can just do sender.idCode. Is this the way to do it?
Is there a better standard/elegant way of implementing this kind of multiple target-action see-where-it-came-from behaviour?
P.S.: Is there a quick way to type the backtick on a Mac?
You could do it this way. But this is not necessary - every UIView (and subclasses which includes UIButton) has the tag property which is just what you want.
UIButton as well as all UIView subclasses already has an integer property for exactly that purpose: tag

Three20 TTSectionedDataSource row height

I'm using Three20 to create a table with several textfields for user registration. I've found two possible methods using Three20. The first uses the TTSectionedDataSource's tableDidLoadModel method to manually add UI components and the second adds custom items that contains pre formatted UI components. The second option seems way more complex and I'm having a difficult time accessing the individual fields. So if one field is a textfield for the username, I need to access the field to submit the username and it doesn't seem like there's an easy answer. The first option gives me a lot of flexibility, but I can't figure out how to set the individual row heights. One row may have a label above a text field, another may have an image, etc. Is there a method that can be used in TTSectionedDataSource that will allow me to set the height for each row? Thus far, I'm using method one and creating UIViews to hold a label field and a text field. I've tried changing the frame of the uiview before it is added to the items array, but it has no affect.
Any ideas?
I believe I may have figured it out. Not sure if this is the correct solution, but it seems to be working.
First in my custom item class I pass the datasource as a delegate. Now that the delegate is part of the item, I can pass it to my textfield as the delegate. As long as I include UITextFieldDelegate in my data source class, it will respond as the delegate to my textfield. So that's getting the content from the textfield.
If I want to change the content in a textfield from the datasource, I can leverage the method:
- (void)tableView:(UITableView*)tableView cell:(UITableViewCell*)cell willAppearAtIndexPath:(NSIndexPath*)indexPath
I can check the row using indexPath.row then type the cell as the corresponding custom cell class. From there I can access any public methods in my cell class. So I created one that returns a reference to the textfield. So I can say:
[[(MyCustomTextFieldCell *)cell theTextField] setText:#"hello world"];
Next step is to assign it to a local ivar and then I'm assuming I should be able to access it at any time.
The main reason I want to be able to modify the content of the textfield is that in certain instances by clicking on the textfield, a picker will come up and the results of the picker are formatted and inserted back into the textfield.
Please let me know if my approach is too convoluted. Perhaps I'll create a sample and post it for everyone to rip apart and tell me I'm a moron and there's a better way.
thanks,
howie

UITextField in UITableViewCell's and proper usage

I have a complex settings style table where individual cells represent different aspects of a data model class. Users can click into a cell and edit individual attributes, such as say if I have a user class, a name, date of birth, etc. My question is, do I need to have an instance of UITextField for each unique cell? Can I just create one subclass of UITableViewCell, set up a delegate, and determine where it is from there?
What's the best approach?
I would recommend creating a subclass of a UITableViewCell. You could do this either purely programaticaly, or if you have an aversion to CGRect's (or want to be able to drag and drop your layout around) with a combination of a XIB and a custom class file.
The Subclass would then contain the UITextFiled's you need, and can also have a delegate or datasource that you can use to point it to your data model object.
It's better to have the UITableViewController you are using act as the text view delegate for each cell - make sure you are re-using cells and when you create them or reuse them attach your class as the delegate for the UITextViews you have via a custom UITableViewCell class with accessors to get to the UITextViews.
If you set cell classes as text delegates you may run into issues if the user scrolls a table view cell off screen with the keyboard up.