ios - 推送到 TableView Controller 后看不到 View Controller 内容

标签 ios objective-c

我是 iOS 和 Objective-C 世界的新手,我想创建一个示例应用程序来学习一些东西,但我发现我的程序存在一些问题。

我创建了一个带有书籍名称的 TableViewController,在触摸一本书之后我想看到关于这本书的更多信息。

所以我创建了 book 类什么是 View Controller ,我在这个类中创建了一些带有一些文本的标签。

NSLogs 工作正常。触摸记录后,从 TableView Controller 推送到新 View Controller 的应用程序工作正常,但我看不到那里的任何内容,而不是顶部的白色 bg 和后退按钮。

这是书类:

#import "BooksViewController.h"

@interface BooksViewController ()

@end

@implementation BooksViewController

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    if(self){
        self.title = self.bookName;
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *bookNameLabel = [[UILabel alloc] init];
    bookNameLabel.text = self.bookName;
    bookNameLabel.frame = CGRectMake(10, 10, 300, 50);
    [self.view addSubview:bookNameLabel];

    UILabel *authorNameLabel = [[UILabel alloc] init];
    authorNameLabel.text = self.authorName;
    authorNameLabel.frame = CGRectMake(50, 50, 300, 40);
    [self.view addSubview:authorNameLabel];

    UILabel *bookDescLabel = [[UILabel alloc] init];
    bookDescLabel.text = self.bookDesc;
    bookDescLabel.frame = CGRectMake(50, 50, 300, 40);
    [self.view addSubview:bookDescLabel];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

这是我用来创建图书信息窗口的 TableView Controller 中的一个方法:

    - (void)viewDidLoad {
        [super viewDidLoad];


        self.bookNames = @[@"Pan Tadeusz", @"Potop", @"Lalka", @"Uczta dla wron", @"Symfonnia C++"];
        self.authorsName = @[@"Adam Mickiewicz", @"Henryk Sienkiewicz", @"Bolesław Prus", @"George R.R Martin", @"Jerzy Greborz"];
        self.bookDescs = @[@"Opis Pan Tadeusz", @"Opis Potop", @"Opis Lalka", @"Opis Uczta dla wron", @"Opis Symfonnia C++"];


    }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Książka na pozycji %ld tapped",indexPath.row);
    BooksViewController *bookVC = [[BooksViewController alloc] init];
    bookVC.bookName = self.bookNames[indexPath.row];
    bookVC.authorName = self.authorsName[indexPath.row];
    bookVC.bookDesc = self.bookDescs[indexPath.row];
    NSLog(@"Nazwa wybranej książki: %@",bookVC.bookName);
    [self.navigationController pushViewController:bookVC animated:YES];

}

最佳答案

你做错了。

首先,创建一个 iVar 来保存用户选择的索引路径,为此,只需在 .m 文件的最顶部添加一个 NSIndexPath 变量。

@implementation yourControllerNameHere (){    //In your code you will have your controller name, just add the NSIndexpath ;)

    NSIndexPath *selectedPath;
}

然后您需要在选择 tableview 单元格时执行 segue,为此,将您的 -didSelectRowAtIndexPath 方法替换为:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Książka na pozycji %ld tapped",indexPath.row);
    selectedPath = indexPath;  //We're saving the selected path to our instance variable ! This is very important otherwise we can't find it again.
    [self performSegueWithIdentifier:@"fromBooksToDetail"];

}

并在您的 .m 文件中添加 -prepareForSegue 方法;当你第一次创建它时它应该在那里!只需找到它并添加以下内容

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([segue.identifier isEqualToString:@"fromBooksToDetail"]){

     BooksViewController *bookVC =(BooksViewController*)segue.destinationViewController;
     bookVC.bookName    = self.bookNames[selectedPath.row];
     bookVC.authorName  = self.authorsName[selectedPath.row];
     bookVC.bookDesc    = self.bookDescs[selectedPath.row];
     NSLog(@"Nazwa wybranej książki: %@",bookVC.bookName);
   }  
}

现在这将不起作用,除非您在 Storyboard 中的两个 View Controller 之间添加一个 segue 链接,因此只需打开您的 Storyboard 并使用右键单击或 ctrl+左键,将鼠标从你的 TableViewController 到你的 BooksController。不要忘记在右侧面板的 Attributes Inspector 中为其指定正确的 identifier!

请注意,我不会那样命名它们;如果 BooksViewController 是 TableViewController 名称,详细信息页面是“BookDetailViewController”,则更有意义。但这只是一个细节。

一旦你有了 segue 链接、performSegue 调用和 prepareForSegue 方法,你就万事大吉了;)

关于ios - 推送到 TableView Controller 后看不到 View Controller 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078156/

相关文章:

ios - 如何在 swift 中为它的 super View 塑造一个 subview ?

objective-c - 滚动时通过 UIScrollView 的 subview 处理点击事件

ios - NSString/NSMutableString 如何终止?

ios - 显示在标签栏后面的 TableView

ios - 如何在后台下载图像以在iOS中使用imageView进行设置?

android - 在 Xaml 中将图像添加到按钮的背景

ios - Facebook Graph API 更改了今天的结果/帖子?

ios - 在 iOS 中使用自动布局填充 UIScrollView 及其内容

iphone - iOS:如何使UIImage模糊?

ios - 使用 Google Maps SDK 在 map 上绘制错误的多段线