0

コードを読みやすくするために、AlertView を新しいオブジェクトとして作成することにしました。クラス AlertViews を NSObject のサブクラスとして作成しました。2 つの UIAlertView を作成しました。1 つはテキスト フィールドが 1 つ、もう 1 つはプレーンな UIAlertView です。それをテストするために、UIAlertView にテキストを入力したかったので、[OK] をクリックして NSLog を確認します。

AlertViews.h

@interface AlertViews: NSobject 
{ 
   UIAlertView *addSomethingAlertView;
   UIAlertView * showSomethingAV; 
}


@property (nonatomic, retain) UIAlertView *addSomethingAlertView;
@property (nonatomic, retain) UIAlertView *showSomethingAV;

-(void)InitializeAddSomethingAlertView;
-(void)InitializeShowSomethingAV;

AlertViews.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

ViewController.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;

ViewController.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 Text Field に入力されたテキストはどのように使用できますか?

ありがとう!

4

2 に答える 2

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 に答える
0

UPOATE - 私は理解しました。

AlertViews.h

#import"ViewController.h"


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


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

-(void)InitializeAddSomethingAlertView;

AlertViews.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]; }

ViewController.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;

ViewController.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 に答える