2010-11-25 27 views
1

您好訪問串我有兩個視圖控制器:FirstViewController和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的字符串,以便在調試器控制檯上顯示它?

+0

問題解決了,我在的appdelegate宣佈的NSString和我用它作爲一個全局變量:我設置在字符串中第一視圖控制器和I使用appDelegate.prStr – Claudio 2010-11-26 10:58:21

回答

1

第一個問題是你沒有分配它。 即使你分配了,你也應該將其作爲一個單獨對象進行分配。

+0

或者使用的appDelegate讀取它在第二的ViewController – KingofBliss 2010-11-25 17:35:36

1

在你的例子中,你永遠不會真的調用setString,所以這個值並沒有真正設置。

除了從,我覺得這只是不好實踐的研究直接使用propertie的getter作爲IBAction爲(即使IBAction爲等同於無效)

0

ü分配該對象(PR)的內存,並嘗試出來。