0

为了更容易阅读代码,我决定创建 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 文本字段中输入的文本?

谢谢!

4

2 回答 2

0

UPOATE - 我想通了。

警报视图.h

#import"ViewController.h"


@interface AlertViews: NSobject 
{ 
UIAlertView *addSomethingAlertView;
UITextField *addSomethingTF;
}


@property (nonatomic, retain) UIAlertView *addSomethingAlertView;
 @property (nonatomic, strong) UITextField *addSomethingTF;

-(void)InitializeAddSomethingAlertView;

警报视图.m

@implementation 

@synthesize addSomethingAlertView = _addSomethingAlertView;
@synthesize showSomethingAV = _showSomethingAV;
@synthesize addSomething;


-(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]; }

视图控制器.h

#import"AlertViews.h"

@interface ViewController: UIViewController <UIAlertViewDelegate> { 

 AlertViews *newAlert;
 UILabel *testLabel;
}

@property (nonatomic, retain) AlertViews *newAlert;
@property (nonatomic, strong) UILabel *testLabel;
@property (nonatomic, strong) NSString *passString;

-(IBAction)adding:(id)sender;

视图控制器.m

@implementation
@synthesize newAlert = _newAlert;
@synthesize showAlert = _showAlert;
@synthesize testLabel;
@synthesize passString;

-(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

在 AlertViews.m 我添加了以下代码:

 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:
 (NSInteger)buttonIndex { if(addSomethingAlertView) { if(buttonIndex == 1) NSLog(@"testing success");


addSomething = [addSomethingAlertView textFieldAtIndex:0];
addSomething.text =[addSomethingAlertView textFieldAtIndex:0].text;




ViewController * vc = [[MainScreenViewController alloc] init];
vc.passString = addSomething.text;


NSLog(@"%@", grab);

}

然后在 ViewController.m 中添加:

-(void)viewWillAppear:(BOOL)animated {

testLabel.text = passString; }
于 2013-09-08T15:09:31.420 回答
0

请检查您是否能够在执行如下操作后使其工作

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
      //cancel button clicked. 
    }
    else{
      //For getting the text entered in alertview text field, you need to do like this
      UITextField* yourTxt= [alertView textFieldAtIndex:0];
      NSString* txtContent= yourTxt.text;

    }
} 

希望能帮助到你..

于 2013-09-04T11:03:00.130 回答