Disable bar button if file doesn't exist
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 vi开发者_Go百科a 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.
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...
精彩评论