我有一个很大的菜鸟问题。我是iOS
的新人,我正在尝试这样做:
我有两个ViewControllers
。第一个按钮有一个按钮,如果按下它,控制将转到第二个视图控制器。
这有效,但问题是当我尝试在第二个视图控制器中获取数据时。 UITextfield
不起作用。
我正在尝试显示我在标签中插入文本字段的内容。但THE TEXTFIELD不起作用:(
我把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
答案 0 :(得分:0)
在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委托。
将@interface SecondViewController : UIViewController
替换为@interface SecondViewController : UIViewController<UITextFieldDelegate>
点击按钮事件,写下:
self.label.text = self.textfield.text;
告诉我它是否无效。