putting ivars into init

纵饮孤独 提交于 2019-11-27 08:52:01

问题


I have two view controllers: BSViewController which contains the source ivars number and array, and BSotherViewController which as the target needs to receive the ivars . One way of producing the desired result was provided in this question. The suggestion was to compute the values of self.number and self.array in an init that overrides the designated init as shown here.

- (id)init {

    if (self = [super init]) {
        //Set values
        NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
        self.array = _array;
        self.number = 25;
    }
    return self;
}

But I don't know how this solution enables the computation of self.number or self.array within the original method (viewDidLoad); I am only able to get 0 and null for their two values in viewDidLoad with any approach I have tried.

In addition, the following line produces a warning issue that view is an unused variable.

BSViewController *view = [[BSViewController alloc] init];

So I am looking for an approach which first computes my ivars number and array and then executes the init(WithNumber) so that the same variables can be used in the target class BSotherViewController.

I thought a more direct approach would be not use init, but instead to use something like the following initWithNumber but I cannot seem to make that work with all the ARC requirements for using underscores, and my limited understanding of objective-c.

- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
    self = [super init];
    if (self) {
        _number = number;
        _array = array;
        return self;
    }
    return nil;
}

For completeness, I will reproduce below most of the code that was produced in the answer for the previous question.

BSViewController.h

#import <UIKit/UIKit.h>

@interface BSViewController : UIViewController{
   // NSInteger number;
}

@property (nonatomic) NSInteger number;
@property (nonatomic, weak) NSArray * array;
// - (id)initWithNumber:(NSInteger)number array:(NSArray *)array;
@end

BSViewController.m

#import "BSViewController.h"
@interface BSViewController ()
@end
@implementation BSViewController
@synthesize number;
@synthesize array;
/*
- (id)initWithNumber:(NSInteger)number array:(NSArray *)array
{
    self = [super init];
    if (self) {
        _number = number;
        _array = array;
        return self;
    }
    return nil;
}
*/
- (id)init {
    if (self = [super init]) {
        NSArray*  _array = [NSArray arrayWithObjects: @"manny",@"moe",nil];
        self.array = _array;
        self.number = 25;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"self: %@", self);
    BSViewController *view = [[BSViewController alloc] init];  //Warning issued: unused variable
    NSLog(@"self number: %d", self.number);
    NSLog(@"self array: %@", self.array);
}
@end

BSotherViewController.h

#import <UIKit/UIKit.h>
@class BSViewController;
@interface BSotherViewController : UIViewController
@property (strong, nonatomic) BSViewController *aview;
@end

BSotherViewController.m

#import "BSotherViewController.h"
#include "BSViewController.h"
@interface BSotherViewController ()
@end

@implementation BSotherViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    BSViewController *aview = [[BSViewController alloc] init];
    NSLog(@"other view: %@", self.aview);
    NSLog(@"other number: %d", aview.number);
    NSLog(@"other array: %@", aview.array);
}
@end

回答1:


You're using a storyboard segue. The segue will create the destination view controller by loading it from the storyboard. There's no point in creating a -[BSotherViewController initWithNumber:array:] method, because the segue won't use it.

When the user triggers the segue by tapping the button, the system creates an instance of UIStoryboardSegue. This segue object has a destinationViewController property which will (in your case) be the BSotherViewController instance that is about to appear. The system sends a prepareForSegue:sender: message to the source view controller (your BSViewController), and passes the segue object as the first argument.

You need to implement prepareForSegue:sender: in BSViewController. In that method, you have access to both the source view controller (self) and the destination view controller (segue.destinationViewController), so you can pass data from one to the other.

First, add number and array properties to BSotherViewController. Then, add a method like this to BSViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // The identifier here must match the one you assigned to the segue in the storyboard.
    if ([segue.identifier isEqualToString:@"GoingToOtherViewController"]) {
        BSotherViewController *destination = segue.destinationViewController;
        destination.number = self.number;
        destination.array = self.array;
    }
}

Finally, in -[BSotherViewController viewDidLoad], use the values of your new number and array properties to set the content of your views.



来源:https://stackoverflow.com/questions/15372796/putting-ivars-into-init

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!