【发布时间】: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