【问题标题】:NSString from other UIViewController is null来自其他 UIViewController 的 NSString 为空
【发布时间】:2013-03-09 14:47:51
【问题描述】:

谁能解释一下为什么这不起作用?我认为这是一个非常简单的问题,但由于某种原因我没有得到它。

我的问题:我的 firstViewController 中有一个值为“Hello!”的 NSString。现在我想在我的 secondViewController 中显示这个值,但输出为空。

这是我的 NSLog

2013-03-09  foo[95381:c07] value of foo in BITfirstViewcontroller: Hello!
2013-03-09  foo[95381:c07] value of foo in BITsecondViewcontroller: (null)

BITAppDelegate.m

#import "BITAppDelegate.h"
#import "BITfirstViewController.h"

@implementation BITAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    BITfirstViewController *fvc = [[BITfirstViewController alloc]init];

    // Create navigationController and set InputViewController as root for navController
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fvc];

    [self.window setRootViewController:navController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

BITfirstViewController.h

#import <UIKit/UIKit.h>

@interface BITfirstViewController : UIViewController

@property (strong, nonatomic) NSString *foo;

-(IBAction)showSecondView:(id)sender;


@end

BITfirstViewController.m

#import "BITfirstViewController.h"
#import "BITsecondViewController.h"

@interface BITfirstViewController ()

@end

@implementation BITfirstViewController

@synthesize foo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        foo = [NSString stringWithFormat:@"Hello!"];
        NSLog(@"value of foo in BITfirstViewcontroller: %@",foo);

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(showSecondView:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)showSecondView:(id)sender;
{

    BITsecondViewController *svc = [[BITsecondViewController alloc]init];
    [self.navigationController pushViewController:svc animated:YES];
}

@end

BITsecondViewController.h

#import <UIKit/UIKit.h>


@class BITfirstViewController;

@interface BITsecondViewController : UIViewController

@property (nonatomic, retain) BITfirstViewController *fvc;

@end

BITsecondViewController.m

#import "BITsecondViewController.h"
#import "BITfirstViewController.h"

@interface BITsecondViewController ()

@end

@implementation BITsecondViewController

@synthesize fvc;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"value of foo in BITsecondViewcontroller: %@", fvc.foo);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@结束

【问题讨论】:

  • 您正在添加第二个视图控制器,但您从未在其上设置fvc,因此它为空。

标签: iphone ios objective-c uiviewcontroller


【解决方案1】:

在 BITfirstViewController.m 中试试这个

-(IBAction)showSecondView:(id)sender;
{

    BITsecondViewController *svc = [[BITsecondViewController alloc]init];
    svc.fvc = self;  // you need to set this property as you prepare the svc
    [self.navigationController pushViewController:svc animated:YES];
}

编辑:

您的fvcsvc 是封装的,除了使用属性(至少这是我们尝试在OOP 中做事的方式)外,没有直接的相互了解。因此,您在svc 中创建@property 是正确的,它允许您拥有fvc 的句柄,但[[BITsecondViewController alloc]init] 只会将该指针初始化为nil。然后您需要将该属性设置为指向预期的对象,在本例中为fvc。由于svc 是从BITFirstViewController 的实例内部构建的,因此要传入的正确句柄是self

【讨论】:

  • 成功了!谢谢,但你愿意(简单地)解释一下吗?
  • 非常感谢!我知道我错过了一些东西。我一直试图让这个工作好几天了。我尝试了几种方法,但直到今天我才意识到我的 UIViewcontroller 是问题所在。所以我创建了这个“svc”和“fvc”设置。我只是认为我的 NSMutableArray 没有在我的其他程序中正确设置。因为回到我的“fvc”时所有条目都消失了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多