Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with other answers that you should probably disable the control if you don't want it to be activated twice. However, if you do want an answer for your actual question about a generic pattern that you can use on all controls then you can use associated objects...</p> <pre><code>- (IBAction)buttonAction:(UIButton*)sender { NSString* webViewKey = @"AssociatedWebView"; // See if there is web view already id webView = objc_getAssociatedObject(sender, webViewKey); if(webView == nil) { // There is no existing web view, create it webView = [self theWebView]; // Associate it with the button objc_setAssociatedObject(sender, webViewKey, webView, OBJC_ASSOCIATION_RETAIN); // Add the web view [self.view addSubview:webView]; } } </code></pre> <p>The above shows a generic way to associate an object to an instance of <code>UIButton</code> so you can check if it is already associated and re-use the existing one. I provide this answer in case you intend to use this in some other way that isn't fully described in your question, but in practice, you could use a property of your controller for the <code>webView</code> that lazy-loads the <code>webView</code> if it isn't already loaded.</p> <p>If you <strong>really</strong> want to simulate the singleton style that you discuss in your question (so that you can have many <code>UIButton</code> instances that all share the same <code>webView</code> object if it already exists) then you could associate the <code>webView</code> onto the <code>[UIButton class]</code> object or even the <code>[UIControl class]</code> object instead of your specific instance. You would do that by replacing the <code>sender</code> with <code>[UIControl class]</code> in the code above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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