Pan*_*ros 15 iphone objective-c
在我的应用程序中,我使用UIAlertView向用户显示消息和一些选项.根据按下的按钮,我希望应用程序在对象上执行某些操作.我使用的示例代码是......
-(void) showAlert: (id) ctx {
UIAlertView *baseAlert = [[UIAlertView alloc]
initWithTitle: title
message: msg
delegate:self
cancelButtonTitle: cancelButtonTitle
otherButtonTitles: buttonTitle1, buttonTitle2, nil];
//baseAlert.context = ctx;
[baseAlert show];
[baseAlert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
id context = ...;//alertView.context;
[self performSelectorOnMainThread:@selector(xxx:) withObject: context waitUntilDone: NO];
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将对象作为上下文对象传递给委托?或者其他一些方式?
我可以在委托上添加属性,但许多不同的警报视图正在使用相同的委托对象.出于这个原因,我更喜欢一种解决方案,其中上下文对象附加到UIAlertView实例并作为UIAlertView对象的一部分传递给委托.
Ken*_*ner 12
我仍然认为在本地存储它是最好的解决方案.创建一个类本地NSMutableDictionary变量来保存上下文对象的映射,以UIAlertView作为键存储上下文,将上下文存储为值.
然后,当调用alert方法时,只需查看字典以查看哪个上下文对象是相关的.如果您不想将整个Alert对象用作键,则只能使用UIAlertView对象的地址:
NSString *alertKey = [NSString stringWithFormat:@"%x", baseAlert];
Run Code Online (Sandbox Code Playgroud)
地址应该在电话上保持不变.或者您可以像其他海报建议的那样标记每个警报,并使用标记在地图中查找上下文.
完成后不要忘记清除上下文对象!
mxc*_*xcl 10
一个完整的实现,允许您传递上下文:
@interface TDAlertView : UIAlertView
@property (nonatomic, strong) id context;
@end
@implementation TDAlertView
@end
Run Code Online (Sandbox Code Playgroud)
一个用法示例,请注意我们如何预先指定指针:
@implementation SomeAlertViewDelegate
- (void)alertView:(TDAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"%@", [alertView context]);
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9787 次 |
| 最近记录: |