在 Objective C 中丢失 viewDidLoad 的属性 [英] Losing properties on viewDidLoad in Objective C
问题描述
我正在尝试构建一个简单的应用程序,用户可以在其中从 tableViewController
中选择一个视频,然后加载一个播放视频的视图.我的问题是将视频的 URL 从 tableViewController
传输到 viewController
.
我遵循了本教程,现在我正在尝试调整代码以进行播放视频而不是仅仅显示图像
我正在从 tableViewController
创建一个 viewController
,如下所示:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {wwfpViewController * DVC = [[wwfpViewController alloc] init];NSIndexPath * path = [self.tableView indexPathForSelectedRow];NSString * theVideoName = [videoNames objectAtIndex:path.row];NSString * theVideoURL = [videoList objectAtIndex:path.row];DVC.videoNum = path.row;DVC.videoName = theVideoName;DVC.videoURL = theVideoURL;【DVC播放】;}
然后触发此 viewController
的播放消息,当我 NSLog
从此消息中时,videoURL
存在且正确.>
然后在这个 viewController
上触发 viewDidLoad
,此时当我 NSLog
的 videoURL
是返回为 (null)
.
我像这样声明 videoURL
:
@property (strong, nonatomic) NSString * videoURL;
所以我有几个问题:
- 当
viewDidLoad
被触发时,viewController
是否会丢失其属性? - 是否有更好的方法将属性发送到
viewController
? - 我这样做完全错了吗?
- 我还需要提供更多代码吗?
问题是,尽管您使用的是 segue,但您手动分配/初始化控制器并设置其属性(这当然是完全不同的实例来自将要呈现的那个).
您应该这样做:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {//获取目标控制器//(它将自动从故事板实例化)wwfpViewController * DVC = (wwfpViewController *)segue.destinationViewController;NSIndexPath * path = [self.tableView indexPathForSelectedRow];NSString * theVideoName = [videoNames objectAtIndex:path.row];NSString * theVideoURL = [videoList objectAtIndex:path.row];//设置属性DVC.videoNum = path.row;DVC.videoName = theVideoName;DVC.videoURL = theVideoURL;【DVC播放】;//为什么不在目标控制器的 viewDidLoad 上调用它?}
PS: 在 Objective-C 中,您通常会命名 ivars(按照惯例),以便它们以小写字母开头,而类以大写字母开头.所以 WWFPViewController
和 dvc
将是更合适的名称.
I'm trying to build a simple App where users can choose a video from a tableViewController
, which then loads a view that plays a video. My problem is transferring the URL of the video from the tableViewController
to the viewController
.
I followed this tutorial, and I am now trying to adapt the code to play videos instead of just show images
I am creating a viewController
from a tableViewController
like this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
wwfpViewController * DVC = [[wwfpViewController alloc] init];
NSIndexPath * path = [self.tableView indexPathForSelectedRow];
NSString * theVideoName = [videoNames objectAtIndex:path.row];
NSString * theVideoURL = [videoList objectAtIndex:path.row];
DVC.videoNum = path.row;
DVC.videoName = theVideoName;
DVC.videoURL = theVideoURL;
[DVC play];
}
The play message for this viewController
is then fired, and when I NSLog
from this message the videoURL
is present and correct.
Then viewDidLoad
is fired on this viewController
, and at this point when I NSLog
the videoURL
it is returned as (null)
.
I'm declaring the videoURL
like this:
@property (strong, nonatomic) NSString * videoURL;
So I have a few questions:
- Does a
viewController
lose its properties whenviewDidLoad
is fired? - Is there a better approach to sending properties to a
viewController
? - Am I doing this completely wrong?
- And do I need to provide any more code?
The problem is that despite the fact that you're using segues, you manually alloc/init a controller and set its properties (which of course is a totally different instance from the one that will be presented).
Here is what you should do instead:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Grab the destination controller
// (it will be instantiated from the Storyboard automatically)
wwfpViewController * DVC = (wwfpViewController *)segue.destinationViewController;
NSIndexPath * path = [self.tableView indexPathForSelectedRow];
NSString * theVideoName = [videoNames objectAtIndex:path.row];
NSString * theVideoURL = [videoList objectAtIndex:path.row];
// Set the properties
DVC.videoNum = path.row;
DVC.videoName = theVideoName;
DVC.videoURL = theVideoURL;
[DVC play]; // Why don't you call this on viewDidLoad of the destination controller?
}
PS: In Objective-C you usually name the ivars (by convention) so that they start with a small letter and the Classes with a capital one. So WWFPViewController
and dvc
would be more appropriate names in your case.
这篇关于在 Objective C 中丢失 viewDidLoad 的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!