【问题标题】:UITextField doesn't work in my SecondViewControllerUITextField 在我的 SecondViewController 中不起作用
【发布时间】:2014-02-03 17:23:00
【问题描述】:

我有一个大菜鸟问题。我是iOS 的新成员,我正在尝试这样做:

我有两个ViewControllers。第一个有一个按钮,如果按下它,控制就会转到第二个视图控制器。

这可行,但问题是当我尝试在我的第二个视图控制器中获取数据时。 UITextfield 不起作用。

我正在尝试显示我在标签的文本字段中插入的内容。但是文本字段不起作用:(

我成功地将 IBOutlets 放入 xibs 并将按钮与它们的 IBActions 连接...

这是我的代码:

ViewController.h

    #import <UIKit/UIKit.h>
#import "SecondViewController.h"

@class SecondViewController;

@interface ViewController : UIViewController

@property (strong, nonatomic) SecondViewController *secondViewController;

-(IBAction)buttonClicked:(id)sender;

@end

ViewController.m

 #import "ViewController.h"

@implementation ViewController

@synthesize secondViewController;

-(IBAction)buttonClicked:(id)sender {
    self.secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    [self presentViewController:self.secondViewController animated:YES completion:nil];

}

SecondViewController.h

    #import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController {
    IBOutlet UITextField *textField;
    UIButton *obtener;
    IBOutlet UILabel *label;
}

@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIButton *obtener;
@property (nonatomic, retain) IBOutlet UILabel *label;

-(IBAction)obtenerClicked:(id)sender;

@end

SecondViewController.m

    #import "SecondViewController.h"

@implementation SecondViewController

@synthesize obtener, textField, label;

-(IBAction)obtenerClicked:(id)sender {
    label.text = textField.text;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

@end

【问题讨论】:

  • 定义“不起作用”。
  • 您的意思是您的标签中没有设置文本值??
  • 我在文本字段中写了“某事”。然后我按下按钮,标签继续为空。所以,程序不工作:/

标签: ios iphone objective-c uitextfield


【解决方案1】:

在您的 SecondViewController.h 中,添加此属性:

@property(nonatomic, readwrite) NSString* myString;

现在在 firstViewController 的 onClick 方法中,添加这一行

-(IBAction)buttonClicked:(id)sender {

//....... Previous code....
 self.secondViewController.myString= @"String to be sent";
}

这将为您传递字符串..

如果要在文本字段中传递字符串,请使用:

-(IBAction)buttonClicked:(id)sender {

    //....... Previous code....
     self.secondViewController.textField.text= @"String to be sent";
    }

希望它能满足您的要求。

更新:
执行以下操作以获得预期结果:
1. 使您的 secondViewController 成为 textView 委托。

在你的 secondViewController.h 将@interface SecondViewController : UIViewController 替换为@interface SecondViewController : UIViewController&lt;UITextFieldDelegate&gt;

  1. 在您的 secondViewController 中放置一个按钮并为其创建一个 IBAction。
  2. 在按钮的 Click 事件中,这样写:

    self.label.text=self.textfield.text;

如果它不起作用,请告诉我。

【讨论】:

  • 你好朋友,谢谢你的回答。但这不是我的问题。我不想在视图控制器之间发送数据。我想在第二个视图控制器中设置文本字段,并使用 IBAction 设置标签。您的解决方案是将数据发送到文本字段,我希望用户自己输入。
  • @Eladar 你在点击 textField 时得到了键盘吗?
  • 我用的是模拟器的键盘,我也试过用我的真键盘,但还是一样,问题依旧。
  • @Eladar.. 所以,当您从键盘输入时,您是否看到 textField 中出现了一些文本?
  • 是的,但是当我按下按钮时,标签没有设置文本。
猜你喜欢
  • 2014-11-18
  • 1970-01-01
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多