IPhone development (4) Advanced Programming customization UIButton case implementation

Source: Internet
Author: User
Tags uicontrol

IPhone DevelopmentProgramming CustomizationUIButtonThe case implementation is the content to be introduced in this article. It mainly describes how to automatically createUIButtonInstead of using XIB files. Through this section, we can learn how to use the addTarget method of UIControl and corresponding event actions instead of InterfaceBuilder.

The specific example is based on the CustomViewController class in the previous article. When the button is pressed, the counter is incremented and displayed in the view.

First, add the technology variable count in the CustomViewController class.

 
 
  1. @ Interface CustomViewController: UIViewController {
  2. Int count; // counter variable.
  3. }
  4. @ End

Next, add the method called when the button is pressed.

 
 
  1. -(Void) countup :( id) inSender {
  2. Count ++; // counter auto-Increment
  3. // InSender is the instance of the Button to be clicked, and its title is set below
  4. [InSender setTitle: [NSString
  5. StringWithFormat: @ "count: % d", count]
  6. ForState: UIControlStateNormal];
  7. }

The setTitle method sets the UIButton title. Use forState: to specify the title display status press, pop up, usually), here specify the title of the general status display. Of course, you can also use UIControlStateNormal.

When the Registration button is pressed, the event function can inherit the UIControl class through addTarget: action: forControlEvents: Method UIButton in the UIControl class, so you can use it directly ). As follows:

 
 
  1. -(Void) viewDidLoad {
  2. [Super viewDidLoad];
  3. Self. view. backgroundColor = [UIColor blueColor];
  4. UIButton * button = [UIButton buttonWithType: UIButtonTypeInfoLight];
  5. Button. frame = CGRectMake (100,100,100,100 );
  6. // Processing function when the Registration button is pressed
  7. [Button addTarget: self action: @ selector (countup :)
  8. ForControlEvents: UIControlEventTouchUpInside];
  9. [Self. view addSubview: button];

ForControlEvents: If UIControlEventTouchUpInside is set in, the response is returned when the button is pressed.

Because the Action Function countup) is of

 
 
  1. -(void)countup:(id)inSender  

When registering, you need to write countup :.

If the function type is

 
 
  1. -(void)countup  

The value is countup. The function type received by addTarget is as follows:

 
 
  1. - (void) countup:(id)sender forEvent:(UIEvent *)event  

You can also register multiple actions for the same response. For example, the following code registers the above two types of action functions:

 
 
  1. // Method 1
  2. -(Void) countup :( id) inSender {
  3. Count ++;
  4. [InSender setTitle: [NSString
  5. StringWithFormat: @ "count: % d", count]
  6. ForState: UIControlStateNormal];
  7. }
  8.  
  9. // Method 2
  10. -(Void) countup {
  11. Count ++;
  12. }
  13.  
  14. -(Void) countup :( id) inSender forEvent :( UIEvent *) event {
  15. Count ++;
  16. [InSender setTitle: [NSString
  17. StringWithFormat: @ "count: % d", count]
  18. ForState: UIControlStateNormal];
  19. }
  20.  
  21. -(Void) viewDidLoad {
  22. [Super viewDidLoad];
  23. Self. view. backgroundColor = [UIColor blueColor];
  24. UIButton * button = [UIButton buttonWithType: UIButtonTypeInfoLight];
  25. Button. frame = CGRectMake (100,100,100,100 );
  26. // Register the first method
  27. [Button addTarget: self action: @ selector (countup :)
  28. ForControlEvents: UIControlEventTouchUpInside];
  29. // Register the second method
  30. [Button addTarget: self action: @ selector (countup)
  31. ForControlEvents: UIControlEventTouchUpInside];
  32. [Button addTarget: self action: @ selector (countup: forEvent :)
  33. ForControlEvents: UIControlEventTouchUpInside];
  34. [Self. view addSubview: button];
  35. }

After compilation, It is shown as follows:

Summary:IPhone DevelopmentProgramming CustomizationUIButtonThe implementation of the case is complete. I hope this article will help you! For more information, see the following articles:

IPhone Development (advanced tutorial) Implementation of iPhone application project composition

IPhone Development 2) iPhone application startup process

IPhone Development (advanced 3) programming and custom UIViewController case implementation

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.