开发者

initialise variable before viewdidload method

开发者 https://www.devze.com 2023-01-22 04:26 出处:网络
i\'m clicking on a row in a table and going to a new view, in the viewdidload i\'m taking the nav.title and putting it in a url to send to a php page. However the url is sending before the nav.title i

i'm clicking on a row in a table and going to a new view, in the viewdidload i'm taking the nav.title and putting it in a url to send to a php page. However the url is sending before the nav.title is initialised, so i'm receiving null back from php. How do i make sure the nav.title is initialised before viewdidload, or can someone think of a better way to structure this code? than开发者_如何学JAVAks


The viewDidLoad method is in UIViewController. Something I find tricky with view controllers is that sometimes you instantiate them in code, and sometimes you instantiate them from a NIB-file.

These two paths of instantiation do not share a common method for proper initialization. So I have taken into practice to alway implement all my UIViewController subclasses using a patter like this:

- (void)primitiveInit {
    // Here I setup all my instance variables.
}

// Designated initializer for instantiating in code.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self) {
        [self primitiveInit];
    }
    return self;
}

// Always called when instantiated from NIB.
- (void)awakeFromNib {
    [super awakeFromNib];
    [self primitiveInit];
}

This way I can always trust primitiveInit to be called on my view controller before they are displayed on screen, and receive any external events.

What I have done is actually that I have added a category on NSObject that calls primitiveInit from both the root class init method, and from the root class awakeFromNib method. This way I only need to implement primitiveInit in any of my classes and never care of how they are instantiated. But this is slightly more advanced, explaining how it works is a topic on it's own.

@implementation NSObject (CWPrimitiveInit)

+ (void)load {
    Method m1 = class_getInstanceMethod(self, @selector(init));
    Method m2 = class_getInstanceMethod(self, @selector(cw_init));
    method_exchangeImplementations(m1, m2);
    m1 = class_getInstanceMethod(self, @selector(awakeFromNib));
    m2 = class_getInstanceMethod(self, @selector(cw_awakeFromNib));
    method_exchangeImplementations(m1, m2);
}

- (void)primitiveInit {
}

- (id)cw_init {
    self = [self cw_init];
    [self primitiveInit];
    return self;
}

- (void)cw_awakeFromNib {
    [self cw_awakeFromNib];
    [self primitiveInit];
}

@end


I have no experience with getting php in an app. But I would try to don't set the content of the URL with the current content of the nav.title. You could try to do this before your new view will open.

  1. Initialize your view controller when the user tabs a cell in your tableviewcontroller
  2. set the URL-variable of your view controller with the right set-methode
  3. After setting it load the new view - it should have now the right link to the webpage independent from the title of the nav bar

I hope this helps... Bye

0

精彩评论

暂无评论...
验证码 换一张
取 消