Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using tags is a little clunky. You have another thing to keep track of. There are two ways of doing this.</p> <p><strong>First Way</strong></p> <p>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 <code>openGoogleInWebview</code> is a lot more informative than <code>buttonAction</code></p> <p>So with this - wire up each buttons action (it's quite easy in Interface Builder) to each action:</p> <pre><code>- (IBAction)openGoogleInWebview; - (IBAction)openYouTubeInWebview; </code></pre> <p>etc.</p> <p>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</p> <pre><code>- (IBAction)openGoogleInWebview { NSURL *url = // Your URL here; [self openWebviewWithURL:url]; } ... - (void)openWebviewWithURL:(NSURL *)url { // load your webview with the url } </code></pre> <p>Keeps things compartmentalised, readable and easy to maintain.</p> <p><strong>Second Way</strong></p> <p>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.</p> <p>If you create IBOutlets for your buttons in your view controller, all you need to do is check the pointer:</p> <p>.h</p> <pre><code>@property (weak, nonatomic) IBOutlet UIButton *googleButton; @property (weak, nonatomic) IBOutlet UIButton *youTubeButton; </code></pre> <p>.m</p> <pre><code>- (IBAction)buttonClicked:(id)sender { if (sender == googleButton) { // load Google } else if (sender == youTubeButton) { // load YouTube } // etc ... } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload