Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are more ways on how to approach this - depending on what exactly you would like to achieve.</p> <p>If this methods are tied with U<code>IViewController</code>'s life and data you would probably want to subclass <code>UIViewController</code> or make an <code>UIViewController</code> category.</p> <p><strong>A: Subclassing</strong> (you want to add some custom properties, variables, methods or you want to override a method):</p> <p><strong>MySubclassedViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface MySubclassedViewController : UIViewController @property (copy) NSString *myVerySpecialString; -(void) myVerySpecialMethod; @end </code></pre> <p><strong>MySubclassedViewController.m</strong></p> <pre><code>#import "MySubclassedViewController.h" @implementation MySubclassedViewController -(void) initialization { self.myVerySpecialString = @"initialized"; } - (id)initWithCoder:(NSCoder*)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self initialization]; } return self; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initialization]; } return self; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { [self initialization]; } return self; } -(void) viewDidLoad { [super viewDidLoad]; [self myVerySpecialMethod]; } -(void) myVerySpecialMethod { if ([self.myVerySpecialString isEqualToString: @"initialized"]) { self.backgroundColor = [UIColor yellowColor]; } } @end </code></pre> <p><strong>B: Category</strong> (you just want to add some extra method to a class):</p> <p><strong>UIViewController+SpecialCatrgory.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface UIViewController (SpecialCategory) -(void) myVerySpecialMethod; @end </code></pre> <p><strong>UIViewController+SpecialCatrgory.m</strong></p> <pre><code>#import "UIViewController+SpecialCatrgory.h" @implementation UIViewController (SpecialCategory) -(void) myVerySpecialMethod { self.backgroundColor = [UIColor yellowColor]; } @end </code></pre> <p><strong>C: Dedicated helper class</strong></p> <p>On the other hand if you find yourself using some independent method on more than one place you might want to consider writing up a helper class and use it as a singleton. </p> <p><strong>MyHelperClass.h</strong></p> <pre><code>@interface MyHelperClass : NSObject + (instancetype)sharedHelper; -(NSString *) myVerySpecialMethod; @end </code></pre> <p><strong>MyHelperClass.m</strong></p> <pre><code>#import "MyHelperClass.h" @implementation MyHelperClass + (instancetype)sharedHelper { static MyHelperClass *_sharedHelper = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ _sharedHelper = [[MAConnectionClient alloc] init]; }); return _sharedHelper; } -(NSString *) myVerySpecialMethod { return @"a result from your very special method"; } @end </code></pre> <p>You use it simply by importing <code>MyHelperClass.h</code> (or putting it in <code>-Prefix.pch</code>), without explicitly creating an instance. For example:</p> <pre><code>NSString *someString = [[MyHelperClass sharedHelper] myVerySpecialMethod]; </code></pre>
    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. 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.
 

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