2

在我正在开发的应用程序中,我有一个视图,当用户将文档导入应用程序时,该视图首先出现。我想要的是出现一个对话框(可能是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 选项中开始导入时在应用程序委托中调用它:

- (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;
        });
    });
}
4

1 回答 1

3

You could define your NSURL as an ivar, and call your handleImportURL: method in the appropriate clickedButtonAtIndex: method like this:

@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
    }
}
于 2013-08-15T13:48:42.527 回答