为了更容易阅读代码,我决定将AlertView创建为新对象。 我创建了类AlertViews作为NSObject的子类。 我创建了2个UIAlertView - 一个是单文本字段,另一个是普通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"); }
}
但是,我没有得到任何LOG。
另外,如何使用在AlertView文本字段中输入的文本?
谢谢!
答案 0 :(得分: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;
}
}
希望有所帮助......
答案 1 :(得分: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; }