在基于导航的应用程序中使用 UIVIewController 填充 UIView

发布于 2024-11-23 16:15:59 字数 1604 浏览 0 评论 0原文

我正在制作一个测试应用程序,用于在基于导航的应用程序中列出电影,使用的是我在 application:didFinishLaunchingWithOptions: 的 appDelegate 中读取的 plist 文件中的数据。当我尝试转到第二个detailViewController时,标题部分发生了变化(我得到了电影的标题,但不是 UIView.title 中的年份,而是得到了类似“78898320”和空的内容) UIImageView 和 UILabels。我缺少

什么 src="https://i.sstatic.net/GjhRR.png" alt="从我的应用程序detailViewController 填充">

这是我在rootViewController 中的方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"YOU SELECTED: %d",indexPath.row);

detailViewController *detailView = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:
                      [movies objectAtIndex:indexPath.row]];

[detailView fill:dict];

[self.navigationController pushViewController:detailView animated:YES];
[detailView release];

}

这是我在detailViewController 中填充UIView 的方法:

-(void)fill: (NSDictionary*) fillData
{
//movie = [NSDictionary dictionaryWithDictionary:fillData];
NSLog(@"DETAILS: %@",[fillData objectForKey:@"description"]);

self.title = [NSString stringWithFormat:@"%@ (%d)",
              [fillData objectForKey:@"title"],
              [fillData objectForKey:@"year"]];
//[movie objectForKey:@"title"];
self.img = [[UIImageView alloc] initWithImage:
            [UIImage imageNamed:[fillData objectForKey:@"image"]]];
self.description = [fillData objectForKey:@"description"];
self.cast = [fillData objectForKey:@"cast"];
}

当我尝试时使用 NSLog 打印字典中的描述,它打印得很好。

I'm making a test application for listing movies in a Navigation Based Application using data from a plist file that I read in my appDelegate in the application:didFinishLaunchingWithOptions:. When I try to go to the second detailViewController I get the title partially changed (I get the title of the movie but instead of the year in the UIView.title I get something like "78898320" & empty UIImageView & UILabels. What am I missing?

Populated from my app rootViewController

Populated from my app detailViewController

Here is my method in the rootViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
NSLog(@"YOU SELECTED: %d",indexPath.row);

detailViewController *detailView = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:
                      [movies objectAtIndex:indexPath.row]];

[detailView fill:dict];

[self.navigationController pushViewController:detailView animated:YES];
[detailView release];

}

Here is my method for filling the UIView in the detailViewController:

-(void)fill: (NSDictionary*) fillData
{
//movie = [NSDictionary dictionaryWithDictionary:fillData];
NSLog(@"DETAILS: %@",[fillData objectForKey:@"description"]);

self.title = [NSString stringWithFormat:@"%@ (%d)",
              [fillData objectForKey:@"title"],
              [fillData objectForKey:@"year"]];
//[movie objectForKey:@"title"];
self.img = [[UIImageView alloc] initWithImage:
            [UIImage imageNamed:[fillData objectForKey:@"image"]]];
self.description = [fillData objectForKey:@"description"];
self.cast = [fillData objectForKey:@"cast"];
}

When I try to print the description from the dictionary using NSLog it prints out fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2024-11-30 16:15:59

您正在使用 %d 作为您使用的对象。

[fillData objectForKey:@"year"]

您应该使用 %@ 来代替,因为它很可能是一个 NSNumber 对象。

You are using %d for the object that you get using

[fillData objectForKey:@"year"]

You should use %@ instead as it is most likely an NSNumber object.

メ斷腸人バ 2024-11-30 16:15:59

您将必须更改一些内容才能使其正常工作。我猜你所有的标签和图像视图实际上都在 NIB 文件中。

通过这样做:

self.img = [[UIImageView alloc] initWithImage:
            [UIImage imageNamed:[fillData objectForKey:@"image"]]];

您实际上是在断开 NIB 文件中的属性和对象之间的连接,将其替换为新的实例。除非您手动将新视图添加到视图中,否则不会显示任何内容。

尝试使用:

[self.img setImage:[UIImage imageNamed:[fillData objectForKey:@"image"]]];

这同样适用于:

self.description = [fillData objectForKey:@"description"];
self.cast = [fillData objectForKey:@"cast"];

尝试改为:

[self.description setText:[fillData objectForKey:@"description"]];
[self.cast setText:[fillData objectForKey:@"cast"]];

You will have to change some things to do it working. I guess all your labels and image views are actually in a NIB file.

By doing :

self.img = [[UIImageView alloc] initWithImage:
            [UIImage imageNamed:[fillData objectForKey:@"image"]]];

you are actually breaking the connection between the property and the object in the NIB file, by replacing it with a fresh new instance. Unless you add manually the new one to the view, nothing can appear.

Try using :

[self.img setImage:[UIImage imageNamed:[fillData objectForKey:@"image"]]];

The same applies to :

self.description = [fillData objectForKey:@"description"];
self.cast = [fillData objectForKey:@"cast"];

Try instead :

[self.description setText:[fillData objectForKey:@"description"]];
[self.cast setText:[fillData objectForKey:@"cast"]];
风情万种。 2024-11-30 16:15:59
-(void)fill: (NSDictionary*) fillData
{
    //movie = [NSDictionary dictionaryWithDictionary:fillData];
    NSLog(@"DETAILS: %@",[fillData objectForKey:@"description"]);

    self.title = [NSString stringWithFormat:@"%@ (%d)",
          [fillData objectForKey:@"title"],
          [[fillData objectForKey:@"year"] intValue]];
    //[movie objectForKey:@"title"];
    self.img = [[UIImageView alloc] initWithImage:
          [UIImage imageNamed:[fillData objectForKey:@"image"]]];
    self.description = [fillData objectForKey:@"description"];
    self.cast = [fillData objectForKey:@"cast"];
}

如果您没有在图像视图中获取图像,因为您没有设置其框架并且没有将其添加到视图中(至少在这里)......

-(void)fill: (NSDictionary*) fillData
{
    //movie = [NSDictionary dictionaryWithDictionary:fillData];
    NSLog(@"DETAILS: %@",[fillData objectForKey:@"description"]);

    self.title = [NSString stringWithFormat:@"%@ (%d)",
          [fillData objectForKey:@"title"],
          [[fillData objectForKey:@"year"] intValue]];
    //[movie objectForKey:@"title"];
    self.img = [[UIImageView alloc] initWithImage:
          [UIImage imageNamed:[fillData objectForKey:@"image"]]];
    self.description = [fillData objectForKey:@"description"];
    self.cast = [fillData objectForKey:@"cast"];
}

If you are not getting Image in image view because ur not setting its frame and you are not adding it to view (atleast here)....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文