[英]Copying a Method from ViewController to AppDelegate - NULL
您好,我嘗試將輸入從ViewController中的文本字段復制到AppDelegate。 不,反之亦然。
當我嘗試這樣做時,我NSLog給了我這個:
StruktonTest[8467:11603] (null)
順便說一句,StruktonTest是我的Testapp的名稱。
這是我在ViewController中獲取文本的代碼:
if ([[[self m_textfield] text] length] == 10) {
m_textfield.text=[NSString stringWithFormat:@"%@", m_textfield.text];
NSLog(@"%@", m_textfield.text);
telefoonnummer = m_textfield.text;
我將其復制到我的AppDelegate.h文件中,如下所示:
@interface LocationDelegate : NSObject <CLLocationManagerDelegate>
{
UILabel * resultsLabel;
NSString *phonenumber;
}
- (id) initWithLabel:(UILabel*)label;
-(id)initWithDictionary:(NSDictionary *)dict;
-(id)initWithName:(NSString *)aName;
-(NSDictionary *)toDictionary;
@property (nonatomic , retain) NSString *phonenumber;
這是我來自ViewController的代碼,也許我在這里犯了錯誤:
@interface LocationTestViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UILabel * m_significantResultsLabel;
IBOutlet UISwitch * m_significantSwitch;
IBOutlet UISwitch * m_mapSwitch;
IBOutlet MKMapView * m_map;
IBOutlet UITextField * m_textfield;
NSString *String;
IBOutlet UILabel * mobielnummer;
IBOutlet UILabel * deellocatie;
IBOutlet UILabel * welldone;
IBOutlet UILabel * locatiemaps;
IBOutlet UILabel * mapslocatie;
NSString *telefoonnummer;
LocationDelegate * m_significantDelegate;
// location objects
CLLocationManager* m_gpsManager;
CLLocationManager* m_significantManager;
}
-(IBAction) actionSignificant:(id)sender;
-(IBAction) actionMap:(id)sender;
-(IBAction) actionLog:(id)sender;
-(IBAction)changrGreeting ;
@property (retain, nonatomic) IBOutlet UILabel * m_significantResultsLabel;
@property (retain, nonatomic) IBOutlet UISwitch * m_significantSwitch;
@property (retain, nonatomic) IBOutlet UISwitch * m_mapSwitch;
@property (retain, nonatomic) IBOutlet MKMapView * m_map;
@property (nonatomic ,retain) IBOutlet UITextField *m_textfield;
@property (nonatomic, copy) IBOutlet NSString *String;
@property (nonatomic, retain) IBOutlet UILabel *mobielnummer;
@property (nonatomic, retain) IBOutlet UILabel *deellocatie;
@property (nonatomic, retain) IBOutlet UILabel *welldone;
@property (nonatomic, retain) IBOutlet UILabel *locatiemaps;
@property (nonatomic, retain) IBOutlet UILabel *mapslocatie;
@property (nonatomic , retain) NSString *telefoonnummer;
@end
這是我的AppDelegate.m中的代碼
LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"%@", phonenumber);
所以我在復制此內容時會收到NULL。 有誰知道如何解決這個問題。 將不勝感激。
這段代碼:
LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"%@", phonenumber);
正在創建LocationTestViewController的新實例,然后讀取.telefoonnummer字段,該字段顯然為空。
當然,您可以使用ViewController將字段“提供”給AppDelegete來代替AppDelegate的“讀取”字段嗎?
您可以將NSUserDefaults用作臨時存儲,或在ViewController中使用如下代碼:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.phoneNumber = self.telefoonnummer;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.