2017-03-16 89 views
0

我用單獨的類中的按鈕創建了popupView。當按鈕在彈出視圖中輕敲時,它應該進入完成處理程序。點擊按鈕後調用方法完成塊

例子。 UIAlertAction actionWithTitle:...:handler^{}; 這將打開一個alertView與確定按鈕。當按鈕被點擊時,我開始在完成處理程序中工作。

是否有可能創建這樣的,如果是的話我該如何在Objective-C中創建它。

我知道如何使用@protocol進行創建,併爲該類設置協議並調用方法。我需要使用這個類的許多視圖控制器,所以我不想調用協議方法,我永遠不會實現類。

回答

2

你可以用塊試試這個(目的C)

用block創建一個屬性,在下面的代碼中寫下你創建pop vi的地方EW。

@property (nonatomic, strong) void (^actionHandler)(void); 

在按鍵操作 -

- (IBAction)doneButtonAction:(id)sender { 
    _actionHandler(); 
} 

//當你告訴popView定義

[popView setActionHandler:^{ 
     // DO Whatever you want, you just need to write this whenever you show pop view, rest code only once. 
}]; 
+0

我想這樣感謝您的答覆。 – Jaff

1

要創建一個函數完成處理

- 斯威夫特3 -

func action(completion:@escaping (_:Bool) -> Void) { 

} 

action { (success) in 

} 

- 目標C -

- (void)action:(void (^)(bool success))completionHandler { 

} 

[self action:^(bool success) { 

}]; 
+0

嗨,我該如何使用它。就像當我點擊按鈕,我應該進入完成處理程序 – Jaff

+0

內檢查更新的答案 –

+0

感謝您的答覆。這看起來不像我想要的。但是我可以用這個代碼來做另一項工作,所以我爲你效勞。我想要像阿倫和SAM這樣的答案回答。 – Jaff

1

我已經創建了兩個獨立的類,ViewController.h和PopupViewController.h處理程序

請看代碼:

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 


@end 

ViewController.m

#import "ViewController.h" 
#import "PopupViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)btnOkPressed:(id)sender 
{ 
    PopupViewController *master=[PopupViewController sharedInstance]; 
    [self presentViewController:master animated:YES completion:^{ 

    }]; 
    [master callCompletion:^(NSString *str) { 
     NSLog(@"%@",str); 
     [master dismissViewControllerAnimated:YES completion:^{ 

     }]; 
    }]; 
} 


@end 

PopupViewController.h

#import <UIKit/UIKit.h> 

typedef void(^Completionhandler)(NSString* str); 

@interface PopupViewController : UIViewController 

+(PopupViewController*)sharedInstance; 
-(void)callCompletion:(Completionhandler)handler; 

@end 

PopupViewController.m

#import "PopupViewController.h" 

@interface PopupViewController() 
{ 
    @private 
    Completionhandler _myHandler; 
} 

@end 

@implementation PopupViewController 

+(PopupViewController*)sharedInstance 
{ 
    static PopupViewController *popup=nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     popup = [[PopupViewController alloc] initWithNibName:@"PopupViewController" bundle:nil]; 
    }); 
    return popup; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)btnOkPressed:(id)sender 
{ 
    _myHandler(@"test msg"); 
} 

-(void)callCompletion:(Completionhandler)handler 
{ 
    _myHandler = handler; 
} 

@end 

我希望你已經得到你的答案,如果你有任何疑問,請隨時問我.. :)

+0

@Jaff你能不能把它做成投票,讓更多的人將這些答案作爲正確的答案,同時也幫助我贏得聲譽。 – SAM

+0

感謝您的回覆SAM。我用它作爲UIView太對了? – Jaff

+0

嗯,當然肯定......爲什麼不;):D – SAM