iphone - 根据第一个 View Controller 中选择的行更改第二个 View Controller 中的信息

标签 iphone ios

好吧,首先我得说我在 iOS 领域有点菜鸟。 也就是说,我将进一步解释我的问题。

我正在制作一个应用程序,用户可以在其中获取动物列表。当用户从列表中选择一种动物时 --> 第二个 View Controller 打开,并且 ImageView 必须设置为所选动物+有该动物的描述。

这部分代码在我的viewController2.m的ViewDidLoad中

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setTitle:@"Animal"];   //here the title has to be set to the animal but I will try this later

   // Do any additional setup after loading the view from its nib.
    UIImage * cowImage = [UIImage imageNamed:@"cow.png"];
    UIImage * horseImage = [UIImage imageNamed:@"horse.png"];

    ViewController1 *viewController1 = [[ViewController1 alloc]init];

    characterNumber = viewController1.pickedRow;

    //set the uiimageview based on characternumber
    switch (characterNumber) {
        case 0:
            foto.image = pukkelpopImage;
            detailInfo.text = @"blabla";
            [self.navigationItem setTitle:characterName];
            break;
        case 1:
            foto.image = cactusImage;
            break;
        default:
            break;
    }

}

在我的 ViewController 1.m 中,当选择一个项目时,我有这个方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    ViewController2 *ViewController22 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];

    [[self navigationController] pushViewController:ViewController22 animated:YES];

    pickedRow = indexPath.row;   //As you can see here I try to get which row was tapped


}

问题可能出在哪里? 感谢您的阅读

最佳答案

您正在 viewController2 中分配 viewController1 的新实例,这与推送 viewController2 的 viewController1 实例完全无关。

尝试在 viewController2 中声明一个 NSInteger 属性来存储选取的行,并在将其推送到 didSelectRowAtIndexPath 之前设置 viewController2 实例的值。

在 ViewController2.h 中声明属性:

@property (nonatomic) NSInteger pickedRow;

在 ViewController1.m didSelectRowAtIndexPath 中,在初始化 viewController2 实例后、推送之前设置该属性:

viewController2.pickedRow = indexPath.row;

无需在ViewController2.m中初始化viewController1:

// ViewController1 *viewController1 = [[ViewController1 alloc]init];

变量characterNumber可以用self.pickedRow替换。

关于iphone - 根据第一个 View Controller 中选择的行更改第二个 View Controller 中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966541/

相关文章:

ios - UIActivityIndi​​catorView 在 UITableViewCell 中停止动画

iphone - 使用命令行在 iOS 模拟器上启动应用程序

iphone - 如何在 iPhone 的 Facebook API 中显式获取访问 token ?

ios - 具有未定义类的可写键路径

ios - 我可以使用日期格式mm/dd/yy吗?

iphone - 将加速器用于fft时出现内存警告

ios - 游戏中心排行榜 - 无法将 GKLeaderboardViewController 委托(delegate)设置为自身(CCLayer)

ios - 框架/libswiftAVFoundation.dylib : valid provisioning profile for this executable was not found

ios - 使用watch 2.0上传iOS应用会给ITC.apps.preReleaseBuild.errors.invalidBinary

iphone - 在 UITextView 上使用 NSLayoutConstraint 会将 contentSize 重置为 {0,0}