从另一个类访问字符串的问题 [英] Problem with access to string from another class

查看:66
本文介绍了从另一个类访问字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器:FirstViewController和SecondViewController

Hi i have two view controller: FirstViewController and SecondViewController

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController: {
 NSString *prStr;
  } 

 -(IBAction)setString;
 @property (nonatomic, retain) NSString *prStr;

 @end







 FirstViewController.m

 #import"FirstViewController.h"

 @implementation FirstViewController

 @synthesize prStr;

 -(IBAction)setString{
  prStr = [[NSString alloc]initWithFormat:@"1"];

   }







 SecondViewController.h

#import <UIKit/UIKit.h>

@class FirstViewController;

@interface SecondViewController: {
 FirstViewController *pr;
  } 

 -(IBAction)example;
 @property (nonatomic, retain) FirstViewController *pr;

 @end







 SecondViewController.m

 #import"SecondViewController.h"
 #import "FirstViewController.h"

 @implementation SecondViewController

 @synthesize pr;

 -(IBAction)example{
  NSLog(pr.prStr);
  if([pr.prStr isEqualToString:@"1"]==YES){

     //Do Something            }

   }

当我构建并运行应用程序时,在执行IBAction示例时,在调试器控制台上没有任何显示!!如何从SecondViewController的操作访问FirstViewController的字符串,以便在调试器控制台上显示它??

When i build and run app, at execution of IBAction example, on debugger console doesn't appear anything!! How can access to the string of FirstViewController from action of SecondViewController, in order to display it on the debugger console???

推荐答案

第一个问题是您尚未分配它。
即使分配了它,也应该将其分配为单例对象。

First problem is that you haven't alloced it. Even if you alloc that, you should alloc as a singleton object.

这篇关于从另一个类访问字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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