为了更容易阅读代码,我决定创建 AlertView 作为新对象。我创建了类 AlertViews 作为 NSObject 的子类。我创建了 2 个 UIAlertView - 一个带有单个文本字段,另一个是纯 UIAlertView。为了测试它,我想在 UIAlertView 中输入文本单击 OK 并查看 NSLog。
警报视图.h
@interface AlertViews: NSobject
{
UIAlertView *addSomethingAlertView;
UIAlertView * showSomethingAV;
}
@property (nonatomic, retain) UIAlertView *addSomethingAlertView;
@property (nonatomic, retain) UIAlertView *showSomethingAV;
-(void)InitializeAddSomethingAlertView;
-(void)InitializeShowSomethingAV;
警报视图.m
@implementation
@synthesize addSomethingAlertView = _addSomethingAlertView;
@synthesize showSomethingAV = _showSomethingAV;
-(void)initializeAddSomethingAlertView {
addSomethingAlertView = [[UIAlertView alloc] initWithTitle:@"Something"
message:@"something" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitle:@"OK", nil];
addsomethingAlertViewStyle = UIAlertViewStylePlainTextInput;
[[addSomethingAlertView textFieldAtIndex:0] setDelegate: self];
[[addSomethingAlertView textFieldAtIndex:0] setKeyboardType: UIKeyboardTypeDecimalPad];
[[addSomethingAlertView textFieldAtIndex:0] becomeFirstResponder];
[addSomethingAlertView show]; }
-(void)InitializeShowSomethingAV {
showSomethingAV = [[UIAlertView alloc] initWithTitle:@"show" message:@"show"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitle:@"show", nil];
[showSomethingAV show];
@end
视图控制器.h
#import"AlertViews.h"
@interface ViewController: UIViewController <UIAlertViewDelegate> {
AlertViews *newAlert;
AlertViews *showAlert;
}
@property (nonatomic, retain) AlertViews *newAlert;
@property (nonatomic, retain) AlertViews *newAlert;
-(IBAction)adding:(id)sender;
-(IBAction)showing:(id)sender;
视图控制器.m
@implementation
@synthesize newAlert = _newAlert;
@synthesize showAlert = _showAlert;
-(IBAction)adding:(id)sender {
[self.newAlert = [[AlertViews alloc] init];
[self.newAlert initializeAddSomethingAlertView];
}
-(IBAction)showing:(id)sender {
[self.showAlert = [[AlertViews alloc] init];
[self.showAlert initializeshowSomethingAV];
@end
我试图在 ViewController.m 中实现这段代码
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (addsomethingAlertView) {if (buttonIndex == 1) { NSLog(@"adding"); }
if (showSomethingAV) {if (buttonIndex == 1) {NSLog(@"showing"); }
}
但是,我没有得到任何日志。
另外,如何使用在 AlertView 文本字段中输入的文本?
谢谢!