What is the difference between .tag and tag - iphone-sdk-3.0

I have a query on this. Please look at the sample code below:
UIButton *button;
button.tag = 1;
and
int but = (int)[(UIButton*)sender tag];
The first line I set tag number 1 to button variable, using the .tag method. And in the second line, I used (int)[(UIButton*)sender tag]; to extract and cast the sender into an integer value. My question would be, what is the difference between .tag and tag method?

There should be no difference. Before Objective C 2.0, the dot methods didn't exist; these were added, but they function as shortcuts to the longer bracketed call.
In your particular code example, in the first code block, button doesn't hold a pointer to a button. You'd need to call
UIButton *button = [UIButton buttonWithType:UIButtonRoundedRect];
button.tag = 1;
this would set the tag to 1. The second code block takes an existing button and extracts its tag to an int, as you indicated. A better example of parallel methods would be:
button.tag = 1; and [button setTag:1];, or
int tag = button.tag; and int tag = [button tag];

Related

Best way to create dynamic list of links/buttons in iOS5 view controller

I want to create a dynamic set of links/buttons in an iOS5 view controller and am trying to figure out what might be the best way to do this.
For eg:
Item 1
Item 2
Item 3
:
:
Item N
Each of the Items is a link/button that is clickable and will do some action, like load another screen etc based on the link.
I don't know ahead of time how many items there might be so if all the items don't fit on the screen, I need to be able to scroll to view.
My question:
1. What is a better way of doing this? I could just create the labels and buttons dynamically but this seems rather cumbersome and I'm not entirely sure how I would differentiate between the different buttons (essentially I'd need some index to find out which Item was clicked).
2. Alternatively, I was wondering if I can just render this page as HTML and just have links? I've never done this and not sure how I'd associate a button with a link.
Any suggestions?
AK
You can try to use the tag property to store the index value you need when you create the button. Then evaluate it in the button tap handler by accessing using button.tag.
Maybe you can try Cordova for an HTML based approach. I'm not too familiar with it though, so I can't say for sure.
Hope it helps.
(1) You can assign UIButton tag property based on the button index. If any events were to trigger, you could recognize which button the event belongs to by checking the tag.
Sample :
// Initializing some buttons
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 1;
[button1 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 2;
[button2 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
Selector for button events :
- (void)buttonPushed:(id)sender {
...
if ([sender tag] == 1) {
// do something after button1 event
} else if () {
// do something after button2 event
}
...
}
(2) If you choose to do it in HTML, you could check out CMHTMLView

objective-c "add buttons automatically"

i need any kind of idea.
this is the problem. i am parsing a xml file which contains the url of an image, name of buttons, url for link to another UIwebview or view, etc... those are the important. what i need is; if a read a name of the button, on the screen must to add a new button automatically with its own image and link. i mean if in the xml i have 6 tags with the information mentioned previously, in the screen must to have 6 buttons with image and link. if in the xml exists more they must exist in the screen or uiwebview too.
i appreciate your help or ideas!!
Well, you can begin by using the NSXMLParser to parse the XML and obtain the properties for your buttons.
NSXMLParser Class Reference
Once you know how many tags you need, you can iterate:
for (int i = 0; i < numTags; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// The tag is so when clicked, you can determine which one was pressed
// It would help to have your links stored in an NSArray, so you can pull
// them out by index.
button.tag = i;
[button setTitle:buttonName forState:UIControlStateNormal];
[button addTarget:self action:#selector(openButtonLink:) forControlEvents:UIControlEventTouchDown];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = ...; // How do you want your buttons laid out?
[someView addSubview:button];
}
I would also recommend checking out the class reference for UIButton:
UIButton Class Reference

UIButton with two lines of text in the title (numberOfLines=2)

I'm trying to make a UIButton that has two lines of text in its titleLabel. This is the code I'm using:
UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
[titleButton setTitle:#"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
titleButton.titleLabel.numberOfLines = 2;
[self addSubview:titleButton];
When I try this, the text only appears on one line. It seems the only way to achieve more than one line of text in UIButton.titleLabel is to set numberOfLines=0 and use UILineBreakModeWordWrap. But this doesn't guarantee the text to be exactly two lines.
Using a plain UILabel, however, does work:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:titleLabel];
Does anyone know how to make the UIButton work with two lines? Is the only solution to create a separate UILabel to hold the text, and add it as a subview of the button?
You don't need to add a UILabel to the UIButton. That's just extra objects and work.
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2;//if you want unlimited number of lines put 0
Swift:
button.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
button.titleLabel!.numberOfLines = 2//if you want unlimited number of lines put 0
Updated answer for more recent iOS versions
Since this is the accepted answer, added #Sean's answer here:
Set these properties on the titleLabel of your button.
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.numberOfLines = 2; // if you want unlimited number of lines put 0
Swift 3 and 4:
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.numberOfLines = 2 // if you want unlimited number of lines put 0
Original answer for an older version of iOS
If you want 2 lines of text on top of your UIButton you should add a UIlabel on top of it that does precisely that.
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
titleLabel.text = #"This text is very long and should get truncated at the end of the second line";
titleLabel.numberOfLines = 2;
titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
[myButton addSubview:titleLabel]; //add label to button instead.
Updated for interface builder solution
Added #Borut Tomazin's answer for a more complete answer.
Updated this part again since the answer of #Borut Tomazin was improved.
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line. You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
You can do this much easier, with no code required. In Interface Builder set Line Break on UIButton to Word Wrap. Than you can insert multiple lines of title. Just hit Option + Return keys to make new line.
You will also need to add this to the User Defined Runtime Attribute in Interface Builder:
titleLabel.textAlignment Number [1]
It's that simple. Hope it helps...
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: #"Line1\nLine2" forState: UIControlStateNormal];
To avoid completely the need to edit code, and thus the need to subclass your view, in Xcode5 and greater you can follow Borut Tomazin suggestion:
In Interface Builder (or storyboard) set Line Break to
Word Wrap. Than you can insert multiple lines of title. Just hit
Option + Return keys to make new line.
and then, in the User Defined Runtime Attributes you can add
Key path: titleLabel.textAlignment
Type: Number
Value: 1
Note: this may be not completely "future proof" since we are translating the UITextAlignmentCenter constant into its numerical value (and that constant may change as new iOS versions are released), but it seems safe in the near future.
You can modify the needed value directly from Storyboard.
Select the button, go to the identity inspector and add the following key-value pair in the "User defined runtime attributes" section:

Get UIButton from view programmatically

Im creating and adding a grid of buttons to my custom view keyboardView as follows:
int offset = 0;
for (int row = 0; row<4; row++){
for (int col = 0; col<13;col++) {
offset +=1;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(5+col*65+offset,5+row*65, 60, 60);
[aButton setTitle:myarray[row][col] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[keyboardView addSubview: aButton];
}
}
I need certain buttons to be of different sizes, like the return key or space bar. How can i get a reference to a particular button programmatically, later on in the same method? Is there an easier way than setting the tag and then calling [keyboardView viewWithTag:t]? Becauseint's are going to get confusing.
Thanks.
You could make instance variables like UIButton *spaceBar. if you reach the Button in the two for-Iretations which is thought to be the spacebar just do spacebar = aButton.
So you can later in the method just use this instance Variable which refers to the specified button. ;-)
I hope it's more or less understandable. ^^
You can either do it with UIView tags (which don't have to get confusing, just create an enum), or if you have only a few "special" UIButtons, you can create ivars to keep references to them.

selector keyword

-(void)clicketbutton{
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setTitle:#"Click here" forState:UIControlStateNormal];
[mybutton addTarget:self action:#selector(displayvalue:)forControlEvents:UIControlEventTouchUpInside];
}
-(void)displayvalue:(id)sender{
UIButton *resultebutton= [UIButton buttonWithType:UIButtonTypeCustom];
resultebutton=sender;// pls clear here.. my question here , it it possible or not. if possible how ? NSLog(#" The buttontitile is %# ", [resultebutton.Title] // here also. }
In the above code I create a button and set title as Click here. When I press that button, I want to print Click here, I mean its title. For that my code is here.
iid is the sender, a pointer to the control that's calling your displayvalue method. You're casting the pointer to an integer, and printing the integer result. I'm not sure exactly what you're trying to accomplish, but the fastest way to get an integer from a button to the button's action method is to store it in the tag property.
If you go into a little more detail on what you're working on, I might be able to describe the best way to model that in a Cocoa app. Also, a tip-- be sure to fix any warnings in your code before trying to figure out why something's not working! That id -> int assignment would have made the compiler complain, for example.