0
我是新來的objective-c。我正在做一個模態視圖的應用程序,將數據傳遞給他的父視圖。我實現了一個協議委託,並且數據傳遞正常,但它不會保留在其他方法中,因爲當我嘗試使用傳遞的值進行某些操作時,我的委託方法外部似乎爲空。我在委託方法內部做了一個NSLog,並且值在那裏,但是當我想在另一個方法中使用該值時似乎錯過了。我沒有使用ARC。自定義委託方法觸發正常,但值不會在其他方法中保留
我的主要ViewController.h:
@protocol ModalViewDelegate
- (void)didReceiveWeight:(NSString *)weight andUnit:(NSString *) unit;
@end
@interface LogWeightTableViewController : UITableViewController<ModalViewDelegate> {
NSNumber *weightPassed;
}
@property (nonatomic, retain) IBOutlet UILabel *WeightSelected;
//...
我的主要ViewController.m:
//...this method is triggered ok, and _weightPassed has the value.
- (void)didReceiveWeight:(NSString *)weight andUnit:(NSString *)unit{
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
_weightPassed = [f numberFromString:weight]; //_weightPassed have null in another methods
[f release];
NSString *value = [[[NSString stringWithString:weight] stringByAppendingString:@" "] stringByAppendingString:unit];
[WeightSelected setText:valor];
}
我ModalView.h:
@property (nonatomic, assign) id<ModalViewDelegate> delegate;
我ModalView.m:
- (void)viewDidLoad
{
//...
LogWeightTableViewController *logWeightTVC = [[LogWeightTableViewController alloc]init];
self.delegate = logWeightTVC;
//...when i press a button to go back to main view call the delegate method:
[delegate didReceiveWeight:valor andUnit:unidad];
要回到我的MainViewController.my想要做的事與我_weightPassed:
- (IBAction)saveLogToDataBase:(id)sender {
// ... do some stuff
[row setKg: _weightPassed]; //<-- here _weightPassed is null
我覺得我失去了一些重要的概念什麼的,希望有人能幫助我解決這個
那麼現在你使用_datePassed或_weightPassed? – 2012-07-15 16:12:24
對不起,我現在編輯了代碼,它是_weightPassed。 – sheinix 2012-07-15 16:14:26
也許使用'_weightPassed = [[f numberFromString:weight] retain]'? – 2012-07-15 16:18:37