ios development foundation 1

Keywords: Attribute

Code creation Label and Button

- (void)viewDidLoad {
    [super viewDidLoad];

    // label
    CGRect screen = [[UIScreen mainScreen]bounds];
    CGFloat labelWidth = 90;
    CGFloat labelHeight = 50;
    CGFloat labelTopView = 150;
    CGRect labelFrame = CGRectMake((screen.size.width - labelWidth)/2, labelTopView, labelWidth, labelHeight);
    UILabel * label  = [[UILabel alloc]initWithFrame:labelFrame];
    label.text = @"testhehehe";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];

    // button
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"titt" forState:UIControlStateNormal];
    CGFloat btnWith = 90;
    CGFloat btnHeight = 50;
    CGFloat btnTopView = 600;
    button.frame = CGRectMake((screen.size.width - btnWith)/2, btnTopView, btnWith, btnHeight);
    [self.view addSubview:button];


 // button click event
    [button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];

}

/**
 *Click event response method
 */
-(void)onclick:(id)sender{
    NSLog(@"click button");
}

The static factory method is used to create the button here.

UIButtonType enumeration member:

  • UIButtonTypeCustom custom button

  • UIButtonTypeSystem system default

  • UIButtonTypeDetailDisclosure

  • UIButtonTypeInfoLight light style information button

  • UIButtonTypeInfoDark dark style information button

  • UIButtonTypeContactAdd add contact button

forState button state:
-UIControlStateNormal default
-UIControlStateHightLighted highlight
-UIControlStateDisabled not available
-UIControlStateSelected select state

Access view

Story drag: select outlet

#import "SecondViewController.h"

@interface SecondViewController ()

- (IBAction)test:(id)sender;


@property (weak, nonatomic) IBOutlet UILabel *bigLabel;

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)test:(id)sender {
    self.label.text = @"woshilabel";
}

@end

Code implementation:

#import "FirstViewController.h"

@interface FirstViewController ()
@property (strong,nonatomic)UILabel * label;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // label
    CGRect screen = [[UIScreen mainScreen]bounds];
    CGFloat labelWidth = 90;
    CGFloat labelHeight = 50;
    CGFloat labelTopView = 150;
    CGRect labelFrame = CGRectMake((screen.size.width - labelWidth)/2, labelTopView, labelWidth, labelHeight);
    self.label  = [[UILabel alloc]initWithFrame:labelFrame];
    self.label.text = @"testhehehe";
    self.label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.label];

    // button
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"titt" forState:UIControlStateNormal];
    CGFloat btnWith = 90;
    CGFloat btnHeight = 50;
    CGFloat btnTopView = 600;
    button.frame = CGRectMake((screen.size.width - btnWith)/2, btnTopView, btnWith, btnHeight);
    [self.view addSubview:button];
    // button click event
    [button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];

}

/**
 *Click event response method
 */
-(void)onclick:(id)sender{
    NSLog(@"click button");
    self.label.text = @"xixi";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

Note: the label object here is the week attribute, because the ownership is the storyboard, and it is strong in the code definition. If the attribute is also set to week when the code is defined, the label object will be released after it is created. Strong: it is guaranteed not to be released, and the ownership belongs to the view controller

Keyboard related
/**
 textfield Response keyboard return method
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"textField  get focus and click return");
    // Call this method to turn off the keyboard
    [textField resignFirstResponder];
    return YES;
}

- (void)viewWillAppear:(BOOL)animated{
    // Sign up for keyboard on notifications
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboradDidShow) name:UIKeyboardDidShowNotification object:nil];
    // Register keyboard close notification
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboradDidHide) name:UIKeyboardDidHideNotification object:nil];
}

-(void) keyboradDidShow{
    NSLog(@"Keyboard open");
}

-(void) keyboradDidHide{
    NSLog(@"Keyboard closure");
}

- (void)viewWillDisappear:(BOOL)animated{
    // Cancel broadcast operation
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

Posted by gford on Wed, 01 Jan 2020 01:07:53 -0800