Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

objective c - putting ivars into init

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...