Reputation: 1328
I have one button in sample.xib named "Google", and I want to load google page in sample1.xib using webview. I may have more buttons like fb,youtube,etc. Now, I want that which button is clicked and according to that, sample1.xib display the webpage. What should I do to get text or id of button ?
Upvotes: 3
Views: 7210
Reputation: 1328
I have got the solution....declare variable in second view...and use it in first view....and get tag from first view of tag....:)
Upvotes: 0
Reputation: 150575
Using tags is a little clunky. You have another thing to keep track of. There are two ways of doing this.
First Way
Use a different action method for each button. This may seem like a lot of repeated code, but I believe in writing code that is easier for the human to read, and letting the compiler take care of optimising (at least until I profile) It makes it a lot easier to see exactly what each method does and an action name like openGoogleInWebview
is a lot more informative than buttonAction
So with this - wire up each buttons action (it's quite easy in Interface Builder) to each action:
- (IBAction)openGoogleInWebview;
- (IBAction)openYouTubeInWebview;
etc.
See - and if you want to optimise this you can write a method that takes a URL and opens it in the webview and send the URL to that: For example
- (IBAction)openGoogleInWebview {
NSURL *url = // Your URL here;
[self openWebviewWithURL:url];
}
...
- (void)openWebviewWithURL:(NSURL *)url {
// load your webview with the url
}
Keeps things compartmentalised, readable and easy to maintain.
Second Way
If you really want to go with the single method that depends on the button that is pressed, there is no reason to use the tag of the button. Which isn't maintainable. You already have a unique identifier for each button; it's pointer address.
If you create IBOutlets for your buttons in your view controller, all you need to do is check the pointer:
.h
@property (weak, nonatomic) IBOutlet UIButton *googleButton;
@property (weak, nonatomic) IBOutlet UIButton *youTubeButton;
.m
- (IBAction)buttonClicked:(id)sender {
if (sender == googleButton) {
// load Google
} else if (sender == youTubeButton) {
// load YouTube
} // etc
...
}
Again - more maintainable; you don't have to keep track of the button's tag. And the code is much more readable because you can see that the google button loads the google page, which is cleaner than saying that tag1 opens the google page and tag 1 is supposed to be the tag for the google button.
Upvotes: 3
Reputation: 35131
Set tag
for your buttons and treat it as an ID(Note: only number is valid).
[fbButton setTag:1];
[fbButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//...
[youtubeButton setTag:2];
[youtubeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//...
and your button action:
- (void)buttonAction:(id)sender
{
switch (((UIButton *)sender).tag) {
case 1:
// load facebook view
break;
case 2:
// load youtube view
break;
default:
break;
}
}
Upvotes: 1