嗨,这是我的第一个问题
看我的代码。我不明白为什么我的数组在更改 View 后变为空 第一 View : 我尝试使用委托(delegate),但它不起作用。我的代表永远不会打电话。
:
@interface CaisseViewController : UIViewController <testDelegate>
@property (nonatomic, retain) NSMutableArray *testArray;
-(void)autrePaiementChoisie:(AutrePaiementCaisseTableViewController*)controller selectPaiement:(NSString *)paiement;
@end
米:
@synthesize testArray;
- (void)viewDidLoad
{
testArray = [[NSMutableArray alloc]initWithObjects:@"TEST 1",@"TEST 2", nil];
}
- (IBAction)autre:(id)sender {
NSLog(@"testArray %@",testArray); // OK !
[self performSegueWithIdentifier:@"autre" sender:self];
}
-(void)autrePaiementChoisie:(AutrePaiementCaisseTableViewController*)controller selectPaiement:(NSString *)paiement {
[controller dismissViewControllerAnimated:YES completion:nil];
NSLog(@"TEST ARRAY %@",testArray); // Is NULL
}
//function of my delegate
-(void)sendString:(NSString *)aString {
NSLog(@"string %@",aString); //dont work ! never called
}
SecondView:声明委托(delegate) :
@protocol testDelegate <NSObject>
-(void)sendString:(NSString*)aString;
@end
@property (nonatomic,assign) id<testDelegate>delegate;
米:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *paiement = [self.paiementArray objectAtIndex:indexPath.row];
[self.delegate sendString:[paiement valueForKey:@"nom"]]; //talk with my delegate
// CaisseViewController *caisseView = [[CaisseViewController alloc]init];
//[caisseView autrePaiementChoisie:self selectPaiement:[paiement valueForKey:@"nom"]];
}
最佳答案
testArray 为 nil 的原因是,在第二个 View Controller 中,您在 didSelectRowAtIndexPath 中创建了 CaisseViewController 的第二个实例,它没有执行 viewDidLoad(即使执行了,您也会有一个单独的 testArray 副本)。
作为加载和移动到第二个 View Controller 的一部分,您应该将第一个 View Controller 的实例传递给它(将其保存为弱指针,如委托(delegate))。您可以在第二个 View Controller 的 didSelectRowAtIndexPath 中使用该实例来回调第一个。
关于ios - 更改 View 后数组变为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23818166/