将ivars放入init [英] putting ivars into init

查看:89
本文介绍了将ivars放入init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器:BSViewController包含源ivars numberarray,以及BSotherViewController作为目标需要接收ivars. 此问题提供了一种产生期望结果的方法.建议是计算如下所示,init中的self.numberself.array的值将覆盖指定的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;
}

但是我不知道该解决方案如何在原始方法(viewDidLoad)中实现self.numberself.array的计算;我尝试用任何方法只能在viewDidLoad中获得两个值的0和null.

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.

此外,以下行会产生一个警告问题,即view是未使用的变量.

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

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

所以我正在寻找一种方法,该方法首先计算我的ivars numberarray,然后执行init(WithNumber),以便可以在目标类BSotherViewController中使用相同的变量.

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.

我认为更直接的方法不是使用init,而是使用类似以下initWithNumber的名称,但是我似乎无法满足所有使用下划线的ARC要求,并且我对此的理解有限目标c.

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

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

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

BSotherViewController.h

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

BSotherViewController.m

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

推荐答案

您正在使用情节提要segue. segue将通过从情节提要中加载来创建目标视图控制器.创建-[BSotherViewController initWithNumber:array:]方法没有意义,因为segue不会使用它.

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.

当用户通过点击按钮触发segue时,系统将创建UIStoryboardSegue的实例.此segue对象具有destinationViewController属性,在您的情况下,该属性将是将要出现的BSotherViewController实例.系统将prepareForSegue:sender:消息发送到源视图控制器(您的BSViewController),并将segue对象作为第一个参数传递.

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.

您需要在BSViewController中实现prepareForSegue:sender:.在这种方法中,您可以访问源视图控制器(self)和目标视图控制器(segue.destinationViewController),因此可以将数据从一个传递到另一个.

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.

首先,将numberarray属性添加到BSotherViewController.然后,向BSViewController添加这样的方法:

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;
    }
}

最后,在-[BSotherViewController viewDidLoad]中,使用新的numberarray属性的值来设置视图的内容.

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

这篇关于将ivars放入init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆