Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将一个视图中的对象添加到另一个视图中的可变数组时出现问题_Ios_Objective C_Uitableview_Nsmutablearray - Fatal编程技术网

Ios 将一个视图中的对象添加到另一个视图中的可变数组时出现问题

Ios 将一个视图中的对象添加到另一个视图中的可变数组时出现问题,ios,objective-c,uitableview,nsmutablearray,Ios,Objective C,Uitableview,Nsmutablearray,大家好,我一直在开发一个应用程序,遇到了一些问题。 我的应用程序有两个ViewController。一个是MenuViewController,另一个是MainViewController 我想将字符串从MainViewController传递到MenuViewController中的可变数组,但不知道如何传递 这是我的密码: <MenuViewController.h> #import <UIKit/UIKit.h> @interface MenuViewContro

大家好,我一直在开发一个应用程序,遇到了一些问题。 我的应用程序有两个ViewController。一个是MenuViewController,另一个是MainViewController

我想将字符串从MainViewController传递到MenuViewController中的可变数组,但不知道如何传递

这是我的密码:

<MenuViewController.h>
#import <UIKit/UIKit.h>

@interface MenuViewController : UITableViewController {
    NSMutableArray *secondFavourite;
}

@property (nonatomic, strong) NSMutableArray *secondFavourite;

@end

我使用NSLog检查MainViewController中的menuViewController.SecondFavorite是否成功地将字符串添加到数组中,该数组不是menuViewController中的同一数组吗?为什么menu.tableView不更新并显示添加的新字符串?我很困惑,希望有人能帮助我


感谢阅读。

这与您的菜单viewDidLoad正在覆盖这两行中的值有关:

self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
第一行是将SecondFavorite属性设置为空NSMutableArray实例。在这种情况下,由于viewDidLoad仅在视图加载到内存后调用,因此当您尝试将视图控制器推送到堆栈上时,SecondFavorite属性中的初始值将丢失


相反,您应该将初始化代码移到init方法中。

谢谢@andrew,但很抱歉,我如何将初始化代码移到init方法中?另外,SecondFavorite中的初始值为空,addToFav方法将向SecondFavorite添加一个字符串。我尝试了self.secondfavorite=[[NSMutableArray alloc]initWithArray:secondfavorite];但仍然不起作用。@HenryNgan我相信您在理解代码执行顺序方面遇到了问题。如果您在MenuViewController的viewDidLoad中放置一个断点,并在逐步执行时检查SecondFavorite属性的值,这将是非常有益的。发生的情况是,在视图控制器被推送到导航堆栈之前,不会调用viewDidLoad,因此代码在初始设置属性之后发生。
<MainViewController.h>
#import <UIKit/UIKit.h>
#import <social/Social.h>

@interface MainViewController : UIViewController {
    IBOutlet UIImageView *imagepost;
    UILabel *predictionLabel;
}

- (IBAction)sampleSelector:(UIButton *)sender;
- (IBAction)showAllClick:(id)sender;
@property (nonatomic, retain) IBOutlet UILabel *predictionLabel;
@property (strong, nonatomic) NSArray *predictionArray;
@property (strong, nonatomic) UIButton *menuBtn;
@property (strong, nonatomic) NSMutableArray *fav;
@property (strong, nonatomic) IBOutlet UILabel *favLabel;
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (nonatomic, strong) NSMutableArray *favourite;
@end
<MainViewController.m>

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.predictionArray = [[NSArray alloc] initWithObjects:@"Hey gurl", nil];
}

//Add to favourite

- (void) addToFav {
    self.favourite = [[NSMutableArray alloc] init];
    [self.favourite addObject:self.predictionLabel.text];
    [self.tableView reloadData];
    NSLog(@"%@", self.favourite);
}

//add to favourite button action

- (IBAction)addToFavButton:(id)sender {
    [self addToFav];

//pass data from favourite here to secondFacourite in MenuViewController (found on stack overflow)

    MenuViewController *menuViewController = [[MenuViewController alloc]initWithNibName:@"MenuViewController" bundle:nil];
    menuViewController.secondFavourite = [[NSMutableArray alloc]initWithArray:self.favourite];
    [self.navigationController pushViewController:menuViewController animated:YES];

}
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;