-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.m
84 lines (65 loc) · 2.2 KB
/
MainViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// MainViewController.m
// firstapp
//
// Created by Scott on 4/17/13.
// Copyright (c) 2013 swclark. All rights reserved.
//
#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *label;
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textField = [[UITextField alloc] initWithFrame: CGRectMake(10.0f, 30.0f, 300.0f, 30.0f)];
self.textField.delegate = self;
// changes the border style so the text field appears onscreen
self.textField.borderStyle = UITextBorderStyleRoundedRect;
/*
add the text field to the main view
UITextField and other UI classes are subclasses of UIView and thus,
can be added into the View heirarchy
*/
[self.view addSubview:self.textField];
// create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// set the button's frame
button.frame = CGRectMake(110.0f, 200.0f, 100.0f, 30.0f);
// set the target, action, and control event
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// add the button to the main view
[self.view addSubview:button];
// create the label
self.label = [[UILabel alloc] initWithFrame:(CGRectMake(115.0f, 150.0f, 200.0f, 30.0f))];
self.label.text = @"Hello World!";
[self.view addSubview:self.label];
}
- (void)buttonPressed
{
self.label.text = self.textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// this tells the OS to remove the keyboard from the forefront
[textField resignFirstResponder];
// instead of adding a line break the text field resigns;
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end