如果文件不存在则禁用栏按钮

发布于 2024-10-21 17:41:27 字数 803 浏览 5 评论 0原文

我有一个带有栏按钮的视图,通过 IBAction(showMovie)和 IBOutlet(moviebutton)连接。我已设法使其通过 IBAction 显示电影,但我想检查加载视图时它将显示的电影文件是否存在,如果不存在,请禁用栏按钮。我在 viewDidLoad 中尝试过这段代码,但无济于事:

if([[NSFileManager defaultManager] fileExistsAtPath:[[NSBundle mainBundle] pathForResource:movfilepath ofType:@"mov"]]) {
    moviebutton.enabled = YES;
} else {
    moviebutton.enabled = NO;
}

它没有给我任何错误,它只是不做任何事情。有人知道如何进行这项工作吗?

编辑:nicktmro,我测试过它,它不是零,它说“电影按钮是 [< UIBarButtonItem: 0x464e190>]”。

另一个编辑:我忘了提及,我使用 NSString 变量来存储电影文件的路径(movfilepath),如果我给它一个虚拟字符串(@“dummypath”),它就可以正常工作,但是当我使用 NSString 变量时,它就可以了。不起作用!任何人都对如何使这项工作有任何想法吗?

另一个编辑:来吧!这个问题没有答案吗?我以为这只是一个简单的问题,根本不需要时间来回答。

另一个编辑???:我自己想出了它,出于某种随机原因,该路径对于电影播放器​​功能来说很好,但对于其他所有功能来说都是空的。所以我让我的parentviewcontroller 禁用该按钮。

I have a view with a bar button on it, connected via an IBAction (showMovie) and an IBOutlet (moviebutton). I have managed to make it display a movie via the IBAction, but I would like to check if the movie file it will display exists when the view loads, and, if it doesn't, disable the bar button. I've tried this code in viewDidLoad, to no avail:

if([[NSFileManager defaultManager] fileExistsAtPath:[[NSBundle mainBundle] pathForResource:movfilepath ofType:@"mov"]]) {
    moviebutton.enabled = YES;
} else {
    moviebutton.enabled = NO;
}

It doesn't give me any errors, it just doesn't do anything. Anyone have any idea as to how to make this work?

EDIT: nicktmro, I've tested it, and it isn't nil, it said "Movie Button is [< UIBarButtonItem: 0x464e190>]".

ANOTHER EDIT: I forgot to mention, I'm using an NSString variable to store the movie file's path (movfilepath), it works fine if I hand it a dummy string (@"dummypath"), but when I use the NSString variable it doesn't work! Anyone have ANY kind of idea on how to make this work???

YET ANOTHER EDIT: Come on! Isn't there ANY answer to this? I thought it was just a simple question, that would take no time at all to answer.

YET ANOTHER EDIT???: I've figured it out on my own, for some random reason the path was fine for the movie player function, but null for everything else. So I got my parentviewcontroller to disable the button instead.

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

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

发布评论

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

评论(1

亚希 2024-10-28 17:41:27

检查 movieButton 是否不为零
moviewbutton.enabled = YES;

在 if 测试之前使用 NSLog(@"Movie Button is [%@]", moviebutton); 打印 movieButton。

顺便说一句,您的拼写不一致:一旦您调用按钮 movieButton ,一旦您调用它 moviebutton (全部小写)。

干杯...

Check that the movieButton is not nil in
moviewbutton.enabled = YES;

Print the movieButton with NSLog(@"Movie Button is [%@]", moviebutton); just before your if test.

By the way, you are not consistent with your spelling: once you call your button movieButton and once you call it moviebutton (all lower case).

Cheers...

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