Objective-C syntax-the first iPhone app thing (10)

Source: Internet
Author: User
Tags button type
What about the first iPhone application in objective-C syntax?
If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/432

The objective-C syntax series has been introducing basic grammar-related knowledge in previous articles, but the goal of Learning grammar is to enter the development world of iPhone IOS. From now on, the objective-C syntax document will not be updated. Every effort is made to update iOS game development software development series. This article Momo will introduce you to the use of some basic controls developed by the iPhone and build our first iPhone application. Dear friends, let's warm up first, hey.

Those who have read the android series should be familiar with this interface ~~









How to obtain the screen size of a mobile phone 

// Get the width and height of the screen
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    int screenWidth = size.width;
    int screenHeight = size.height;





1. Text Box View
Add a text box in the view, you can write some content in the box, set the font color, position, size and so on.
    // Create label view
    label = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, screenWidth, 30)];
    // Set display content
    label.text = @ " MOMO's world";
    // Set the background color
    label.backgroundColor = [UIColor blueColor];
    // Set text color
    label.textColor = [UIColor whiteColor];
    // Set the display position to the center
    label.textAlignment = UITextAlignmentCenter;
    // Set the font size
    label.font = [UIFont fontWithName: [[UIFont familyNames] objectAtIndex: 10] size: 20];
2. Button view
       The button type is 1 for ordinary buttons. CGrectMake sets the position and size of the button. The first two parameters set the start X and Y coordinates of the button. The last two parameters set the width and height of the button.
       Here I will focus on addTarget, which can set the binding event of the button. Action: Set the response method after the button is clicked. This line of code means that the program execution method ButtonPressed is the code in the function after the button is clicked.
    // Create button
    button = [UIButton buttonWithType: 1];
    // Set the button range
    button.frame = CGRectMake (0, 40, screenWidth, 30);
    // Set button display content
    [button setTitle: @ "This is a button" forState: UIControlStateNormal];
    // Set the button display color
    button.backgroundColor = [UIColor blackColor];
    // Set the response method after the button is changed
    [button addTarget: self action: @selector (ButtonPressed) forControlEvents: UIControlEventTouchUpInside];

Click this button to enter the following method, a dialog box pops up.

-(void) ButtonPressed
{
  
    // Create dialog
    UIAlertView * alertA = [[UIAlertView alloc] initWithTitle: @ "My view" message: @ "Welcome to learn IPHONE development together" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
    // Add cancel button
    [alertA addButtonWithTitle: @ "Cancel"];
    // Display this UIAlerView
    [alertA show];
    // objective-C does not have its own garbage collection mechanism like java, so we must pay attention to freeing up memory when writing programs, and develop good habits from the beginning
    [alertA release];
   
}






3. Progress bar view
Similar to the construction of the button view above, here you set the maximum and minimum values of the progress bar. When dragging, you can directly get the value between this range, and also bind the drag event to the valueChangeTest method.
    // Create progress bar
    slider = [[UISlider alloc] initWithFrame: CGRectMake (0,80, screenWidth, 30)];
    // Maximum progress bar
    slider.maximumValue = 100;
    // Minimum progress bar
    slider.minimumValue = 0;
    // The position of the starting point
    slider.value = 20;
    // Set the background color
    slider.backgroundColor = [UIColor blackColor];
    // After setting the progress bar change, bind the response method
    [slider addTarget: self action: @selector (valueChangeTest) forControlEvents: UIControlEventValueChanged];

After dragging the progress bar, it changes into the following method, [slider vale] can get the drag progress value.


-(void) valueChangeTest
{
  
    float value = [slider value];
    NSLog (@ "progress bar has changed:% f", value);
    
}










4. edit box view
        Very common view where you can enter information in the edit box. The premise is that the user touches and clicks on the input box. At this time, the system soft keyboard pops up to input information, but this input box will not be automatically closed. We need to call the code in the program to close it. We will introduce how to close this input box later.

    // Create text input box
    textfield = [[UITextField alloc] initWithFrame: CGRectMake (0,120, screenWidth, 50)];
    
    // The default display text
    textfield.text = @ "This is an input box";
    // Show text after clicking
    textfield.placeholder = @ "Please enter information in the input box";
    // Text display position, left aligned here
    textfield.textAlignment = UITextAlignmentLeft;
    // The default display text color
    textfield.textColor = [UIColor grayColor];
    // Set the input font
    textfield.font = [UIFont fontWithName: [[UIFont familyNames] objectAtIndex: 0] size: 17];
    // Set the type of input box, 3 is common type
    textfield.borderStyle = 3;
    // Click the input box to clear the original content
    textfield.clearsOnBeginEditing = YES;
    // Set the background color of the input box
    textfield.backgroundColor = [UIColor blackColor];




5. Picture view Set the position of the picture displayed on the screen. Of course, this picture file must be copied to the project. The copy method can directly drag the picture to the Xcode project with the mouse.
    // Create a picture view
    imageview = [[UIImageView alloc] initWithFrame:
                            CGRectMake (100, 200, 120, 120)];
    
    // Set the resource path of the picture display
    [imageview setImage: [UIImage imageNamed: @ "temp.jpg"]];
6. The transparent full-screen button exists to solve the problem that the input method cannot be automatically closed after the appearance of the input method. That is to say, if the input method soft keyboard appears, click the screen to close the input method at any time. Transparent button, call the close input method method to close the input method.
    // Create a hidden button
    backgroudButton = [[UIButton alloc] init];
    // let this fill the entire screen
    backgroudButton.frame = self.view.frame;
    // Add the response time of the button to close the soft keyboard
    [backgroudButton addTarget: self action: @selector (ButtonClick) forControlEvents: UIControlEventTouchUpInside];
Tap anywhere on the screen to close the input method.

-(void) ButtonClick
{
    // Touch the screen to close the soft keyboard
    [textfield resignFirstResponder];
}


        In this way, the codes of all the views have been posted. These views are actually subViews, and these subViews need to be added to the main view of the screen. And in order to avoid memory leaks, you must release these views in a timely manner.
    // Add all objects to the view
    [self.view addSubview: backgroudButton];
    [self.view addSubview: label];
    [self.view addSubview: imageview];
    [self.view addSubview: button];
    [self.view addSubview: slider];
    [self.view addSubview: textfield];
   

    
    // Release all objects
    [imageview release];
    [label release];
    [slider release];
    [textfield release];

The complete code is given below

HelloWorldViewController.h Controller
#import <UIKit / UIKit.h>

@interface HelloWorldViewController: UIViewController
{
    // text box
    UILabel * label;
    // button
    UIButton * button;
    //progress bar
    UISlider * slider;
    //Input box
    UITextField * textfield;
    // picture view
    UIImageView * imageview;
    // Background button
    UIButton * backgroudButton;
}
@end
HelloWorldViewController.m

#import "HelloWorldViewController.h"

@implementation HelloWorldViewController

-(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


// Implement viewDidLoad to do additional set up after loading the view, typically from a nib.
-(void) viewDidLoad
{
    [super viewDidLoad];
    
    // Get the width and height of the screen
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    int screenWidth = size.width;
    int screenHeight = size.height;
    
    
    
    // Create label view
    label = [[UILabel alloc] initWithFrame: CGRectMake (0, 0, screenWidth, 30)];
    // Set display content
    label.text = @ "雨 松 MOMO 的 世界";
    // Set the background color
    label.backgroundColor = [UIColor blueColor];
    // Set text color
    label.textColor = [UIColor whiteColor];
    // Set the display position to the center
    label.textAlignment = UITextAlignmentCenter;
    // Set the font size
    label.font = [UIFont fontWithName: [[UIFont familyNames] objectAtIndex: 10] size: 20];
    
 
    // Create button
    button = [UIButton buttonWithType: 1];
    // Set the button range
    button.frame = CGRectMake (0, 40, screenWidth, 30);
    // Set button display content
    [button setTitle: @ "This is a button" forState: UIControlStateNormal];
    // Set the button display color
    button.backgroundColor = [UIColor blackColor];
    // Set the response method after the button is changed
    [button addTarget: self action: @selector (ButtonPressed) forControlEvents: UIControlEventTouchUpInside];
    
    
    
    // Create progress bar
    slider = [[UISlider alloc] initWithFrame: CGRectMake (0,80, screenWidth, 30)];
    // Maximum progress bar
    slider.maximumValue = 100;
    // Minimum progress bar
    slider.minimumValue = 0;
    // The position of the starting point
    slider.value = 20;
    // Set the background color
    slider.backgroundColor = [UIColor blackColor];
    // After setting the progress bar change, bind the response method
    [slider addTarget: self action: @selector (valueChangeTest) forControlEvents: UIControlEventValueChanged];
   
    
   
    
    // Create text input box
    textfield = [[UITextField alloc] initWithFrame: CGRectMake (0,120, screenWidth, 50)];
    
    // The default display text
    textfield.text = @ "This is an input box";
    // Show text after clicking
    textfield.placeholder = @ "Please enter information in the input box";
    // Text display position, left aligned here
    textfield.textAlignment = UITextAlignmentLeft;
    // The default display text color
    textfield.textColor = [UIColor grayColor];
    // Set the input font
    textfield.font = [UIFont fontWithName: [[UIFont familyNames] objectAtIndex: 0] size: 17];
    // Set the type of input box, 3 is common type
    textfield.borderStyle = 3;
    // Click the input box to clear the original content
    textfield.clearsOnBeginEditing = YES;
    // Set the background color of the input box
    textfield.backgroundColor = [UIColor blackColor];
   
    
    
    // Create a picture view
    imageview = [[UIImageView alloc] initWithFrame:
                            CGRectMake (100, 200, 120, 120)];
    
    // Set the resource path of the picture display
    [imageview setImage: [UIImage imageNamed: @ "temp.jpg"]];
     
    // Create a hidden button
    backgroudButton = [[UIButton alloc] init];
    // let this fill the entire screen
    backgroudButton.frame = self.view.frame;
    // Add the response time of the button to close the soft keyboard
    [backgroudButton addTarget: self action: @selector (ButtonClick) forControlEvents: UIControlEventTouchUpInside];
    
    
    // Set the background color of the entire view
    [self.view setBackgroundColor: [UIColor blackColor]];
    
    
    
    // Add all objects to the view
    [self.view addSubview: backgroudButton];
    [self.view addSubview: label];
    [self.view addSubview: imageview];
    [self.view addSubview: button];
    [self.view addSubview: slider];
    [self.view addSubview: textfield];
   

    
    // Release all objects
    [imageview release];
    [label release];
    [slider release];
    [textfield release];
    
}

-(void) ButtonPressed
{
  
    // Create dialog
    UIAlertView * alertA = [[UIAlertView alloc] initWithTitle: @ "My view" message: @ "Welcome to learn IPHONE development together" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
    // Add cancel button
    [alertA addButtonWithTitle: @ "Cancel"];
    // Display this UIAlerView
    [alertA show];
    // objective-C does not have its own garbage collection mechanism like java, so we must pay attention to freeing up memory when writing programs, and develop good habits from the beginning
    [alertA release];
   
}


-(void) valueChangeTest
{
  
    float value = [slider value];
    NSLog (@ "progress bar has changed:% f", value);
    
}

-(void) ButtonClick
{
    // Touch the screen to close the soft keyboard
    [textfield resignFirstResponder];
}



-(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 (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


In the end, if you still feel that I have n’t written enough in detail, I do n’t watch it well enough. It does n’t matter if I post the source code. Welcome everyone to discuss and learn from Yusong MOMO. I hope to make progress with you.

: Http://download.csdn.net/detail/xys289187120/3645156

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.