我还是iOS新手,在我正在开发的应用中,当用户从外部源导入文件时,会显示此视图。我希望只有在用户在操作表中确认后才执行导入, 分层结构如下:
用户从他的电子邮件中选择在应用程序中打开文件。
然后,他切换到progressView(在导入过程中以编程方式成为根视图)。
该过程完成,并且默认的rootview被设置回来。
我想要的是询问用户是否真的想在progressView显示时立即导入,如果不是,则必须取消它(我不知道该怎么做)
谢谢,
这是功能:
- (void)handleImportURL:(NSURL *)url
{
__block JASidePanelController * controller = (JASidePanelController *)self.window.rootViewController;
// Show progress window
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
__block PVProgressViewController * progressController = [storyboard instantiateViewControllerWithIdentifier:@"kProgressViewController"];
self.window.rootViewController = progressController;
// I think here somethings needs to be done with UIActionsheet
// 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;
});
});
}
更新:以下是我尝试使用操作表的内容:
//First the function that calls the handleImport
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// Handle CSV Import
if (url != nil && [url isFileURL]) {
[self handleImportURL:url];
[self confirmImportAlert];
}
return YES;
}
//显示操作表
- (void)confirmImportAlert {
UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Proceed through Import?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", @"Maybe", nil];
[myActionSheet showInView: self.window];
}
// what to do
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0){
// Avoid Import
}
else{
[self handleImportURL:_importURL];
//initiate import
}
}
更新2:所以我添加并更改了两个方法(我似乎无法调用app委托中的ActionSheetWilldismiss函数):
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *choice = [ actionSheet buttonTitleAtIndex:buttonIndex];
if(buttonIndex == 0){
//initiate import
[self handleImportURL:_importURL];
}
else{
//don't initiate import
}
}
//This methods creats the action sheet
- (void)confirmImportAlert {
// importActionSheet is a property in the appdelegate.h
if (!self.importActionSheet){
UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Proceed through Import?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", nil];
[myActionSheet showInView: self.window];
myActionSheet.delegate = self;
self.importActionSheet = myActionSheet;
}
}
并将调用import的函数更改为:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// Handle CSV Import
if (url != nil && [url isFileURL]) {
[self confirmImportAlert];
//[self handleImportURL:url];
}
return YES;
}
答案 0 :(得分:0)
您应该在方法中创建并显示UIActionsheet
,但不要执行任何当前代码。您应该将所有当前代码移动到另一个方法,如果用户在委托方法actionSheet:willDismissWithButtonIndex:
中点击了正确的按钮,则应该调用该方法。
答案 1 :(得分:0)
无论采取什么操作触发,-handleImportURL:
的来电都应该创建一个UIActionSheet
并显示它。
如果是按钮,例如:
-(void)buttonTapped:(UIButton *)sender
{
// [self handleImportURL:someURL];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Confirm",nil];
[actionSheet showInView:self.view];
}
然后你需要实现委托方法:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.cancelButtonIndex != buttonIndex) {
[self handleImportURL:someURL];
}
}