在我开发的应用程序中,我有一个视图,只要用户将文档导入应用程序,该视图就会首次出现。我想要的是出现一个对话框(可能是 UIAlert或活动表单),询问用户是否真的要导入该文件(是或取消)。我想知道在执行导入操作之前如何使UIAlert / Actionsheet菜单显示,以及当用户点击任何一个时如何分配操作或否,或“更改文件
出现的视图称为ProgressBarView.m / h。在它中是viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
self.progressBar.progressTintColor = [UIColor colorWithRed:153.0/255 green:0 blue:0 alpha:1.0];
self.progressBar.progress = 0.0;
/*progressValueLabel = [NSString stringWithFormat:@"%.0f%%", (self.progressBar.progress * 100)];*/
self.progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.3f
target:self
selector:@selector(changeProgressValue)
userInfo:nil
repeats:YES];
}
当在appdidFinishLaunchwith选项中开始导入时,会在app delegate中调用它:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
**[self handleImportURL:url];** // this is the function that handles the import
}
执行导入并调用progressView的函数如下:
- (void)handleImportURL:(NSURL *)url
{
// Show progress window
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
__block PVProgressViewController * progressController = [storyboard instantiateViewControllerWithIdentifier:@"kProgressViewController"];
self.window.rootViewController = progressController;
// Perform import operation
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSError *outError;
NSString * csvString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&outError];
NSArray * array = [csvString csvRows];
[[PVDatabaseController sharedController] importArray:array progressHandler:^(float progress) {
progressController.progressBar.progress = progress;
}];
dispatch_async(dispatch_get_main_queue(), ^{
self.window.rootViewController = controller;
});
});
}
答案 0 :(得分:3)
您可以将NSURL定义为ivar,并使用相应的handleImportURL:
方法调用clickedButtonAtIndex:
方法:
@implementation AppDelegate{
NSURL *_importURL;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_importURL = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (url != nil && [url isFileURL]) {
[self confirmImportAlert];
}
}
UIActionSheet:
- (void)confirmImportAlert {
UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Your Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", nil];
myActionSheet.delegate = self;
[myActionSheet showInView:self.window];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//initiate import
[self handleImportURL:_importURL];
}
else{
//don't initiate import
}
}
UIAlertView中:
- (void)confirmImportAlert {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your Title" message:@"Your Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
[myAlertView show];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
//initiate import
[self handleImportURL:_importURL];
}
else{
//don't initiate import
}
}