ios - 更改 View 后数组变为 Null

标签 ios nsmutablearray

嗨,这是我的第一个问题

看我的代码。我不明白为什么我的数组在更改 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/

相关文章:

javascript - 将第 1 页的值保存到第 2 页 - uiWebView

ios - 如何以面向对象的方式重用这个 ViewController 代码?

ios - NSMutableArray removeObjectAtIndex 在第二次执行时崩溃

iphone - iOS - 无法访问 didSelectRowAtIndexPath 中的 NSMutableArray?

ios - 有没有 `addObject` 的被动方法?

iphone - NSMutableArray 在日志中显示正确值但在表格单元格中不显示

iOS - 更改窗口色调颜色会更改完成/保存按钮的字体粗细

ios - 从 UITableView 移动到 Table Cell 上的 UIView 按

ios - 如何在 iphone 的 UITableview 单元格中设置文本位置数组

ios swift,在 NSArray 中传递 Parse PFbject