我正在尝试展示一个带有包含两个标签的自定义叠加层的相机视图控制器。我一直无法让我的 imagePickerControllerDidCancel 方法和 alertView 正常工作。系统正在向 NSLog 打印正确的信息,但我编写的代码行没有做任何事情,当用户点击“返回”(取消 alertView 并返回到 imagePicker)时,相机再次出现,但 takePhoto 按钮和取消按钮不再可用。它们仍然可以被看到,但不能被点击。当用户单击索引 1(离开)处的按钮时,我希望关闭相机并将用户带回我的 homeViewController,它位于 tabBarController 索引:1。此外,当相机第一次加载时,timerLabel 会显示正确的数据,但很快就会消失。我觉得这与我的 refreshLabel 方法有关,但同样,我并不清楚我哪里出错了。这些东西都不适合我,而且我对编程世界还是很陌生,所以对任何这些问题的帮助将不胜感激。谢谢!
#import "CameraViewController.h"
#import "FriendsViewController.h"
#import <mobileCoreServices/UTCoreTypes.h>
@interface CameraViewController ()
@end
@implementation CameraViewController
@synthesize titleLabel;
@synthesize timerLabel;
- (void)viewDidLoad
{ [super viewDidLoad];
self.recipients = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.image == nil && [self.videoFilePath length] == 0) {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = NO;
self.imagePickerController.videoMaximumDuration = 10;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;}
self.imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePickerController.sourceType];
[self presentViewController:self.imagePickerController animated:NO completion:nil];}
[[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
self.overlayView.frame = CGRectMake(160,8,0,0);
self.imagePickerController.cameraOverlayView = self.overlayView;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *deadline = [NSString stringWithFormat:@"%@/deadline.txt",
documentsDirectory];
NSString *name = [NSString stringWithFormat:@"%@/name.txt",
documentsDirectory];
NSError *fileError;
titleLabel.text = [NSString stringWithContentsOfFile:name
encoding:NSASCIIStringEncoding
error:&fileError];
timerLabel.text = [NSString stringWithContentsOfFile:deadline
encoding:NSASCIIStringEncoding
error:&fileError];
if(fileError.code == 0){
NSLog(@"deadline.txt was read successfully with these contents: %@,",
timerLabel.text);
NSLog(@"name.txt was read successfully with these contents: %@,",
titleLabel.text);}
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(refreshLabel)
userInfo:nil
repeats:YES];
}
-(void)refreshLabel;{
self.formatter = [NSDateFormatter new];
[self.formatter setDateFormat:@"dd:hh:mm:ss"];
NSDate *startDate = [self.formatter dateFromString:self.timerLabel.text];
NSDate *timeLeft = [startDate dateByAddingTimeInterval:-1];
NSTimeInterval totalCountdownInterval = -1;
NSTimeInterval elapsedTime = [timeLeft timeIntervalSinceNow];
NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;
if (remainingTime <= 0.0) {
//dismiss controller and set to homecontroller at tabBar index 1
}
self.timerLabel.text = [self.formatter stringFromDate:timeLeft];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Are you Sure?"
message:@"you can't come back"
delegate:self cancelButtonTitle:@"Back" otherButtonTitles:@"Yes", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.imagePickerController dismissViewControllerAnimated:NO completion:nil];
[self.tabBarController setSelectedIndex:1];
NSLog(@"Leave clicked");
}
else {
[self reset];
NSLog(@"cancel clicked");
}
}
- (void)reset {
self.image = nil;
self.videoFilePath = nil;
}