0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · บทเรียน

ตัวควบคุมและเหตุการณ์ UI ทั่วไป

ทำงานกับตัวควบคุม UIKit มาตรฐาน เช่น ปุ่ม ป้ายกำกับ และช่องข้อความ พร้อมเรียนรู้การจัดการการโต้ตอบและเหตุการณ์ของผู้ใช้

ตัวควบคุมและเหตุการณ์ UI ทั่วไป เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

UI Controls: Your App's Face

User Interface (UI) controls are the interactive elements users see and touch in an app. They are the building blocks of any iOS application's visual experience.

  • Buttons: Tap to perform actions.
  • Labels: Display static text.
  • Text Fields: Input text from the user.

Mastering these controls is crucial for creating intuitive and functional apps, especially when maintaining legacy Objective-C codebases.

Displaying Text with UILabel

A UILabel is used to display static, read-only text in your app. Think of it as a sign or a description.

  • Set its text property to display content.
  • Customize its appearance with properties like textColor, font, and textAlignment.
  • Labels can be single or multi-line.

It's one of the most fundamental UI elements you'll encounter.

UILabel in Action

Let's create a UILabel programmatically and inspect its properties. Remember, in a real app, you'd add this to a view to see it visually!

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Create a label with a frame (position and size)
        UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 280, 40)];
        myLabel.text = @"Hello CoddyKit!";
        myLabel.textColor = [UIColor darkTextColor];
        myLabel.font = [UIFont boldSystemFontOfSize:20.0];
        myLabel.textAlignment = NSTextAlignmentCenter;

        NSLog(@"Created UILabel:");
        NSLog(@"  Text: %@", myLabel.text);
        NSLog(@"  Font Size: %.1f", myLabel.font.pointSize);
        NSLog(@"  Alignment: %ld", (long)myLabel.textAlignment);
    }
    return 0;
}

Making Apps Interactive with UIButton

The UIButton is arguably the most common interactive control. Users tap buttons to trigger actions.

  • Buttons can display text, an image, or both.
  • They have different states (Normal, Highlighted, Disabled, Selected).
  • You attach actions to buttons to define what happens when they're tapped.

Buttons are key for user interaction in any iOS application.

Responding to Button Presses

To make a UIButton do something, you use the Target-Action pattern. This connects a specific event (like a tap) to a method in an object (the target).

  • addTarget:action:forControlEvents: is the method you call.
  • The target is typically your UIViewController or another handler object.
  • The action is a method selector (e.g., @selector(buttonTapped:)).
  • UIControlEventTouchUpInside is the common event for a tap inside the button's bounds.

Implementing a Tap Action

Here's how you'd set up a button and its action. When you "run" this, imagine the button being tapped and the message appearing in the console!

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface ButtonHandler : NSObject
- (void)buttonTapped:(UIButton *)sender;
@end

@implementation ButtonHandler
- (void)buttonTapped:(UIButton *)sender {
    NSLog(@"Button '%@' was tapped!", [sender titleForState:UIControlStateNormal]);
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ButtonHandler *handler = [[ButtonHandler alloc] init];

        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
        myButton.frame = CGRectMake(10, 100, 200, 50);
        [myButton setTitle:@"Press Me!" forState:UIControlStateNormal];
        
        // Set up the target-action. The handler object will receive the message.
        [myButton addTarget:handler 
                     action:@selector(buttonTapped:)
           forControlEvents:UIControlEventTouchUpInside];

        NSLog(@"Button created. If tapped, console would show action.");
        // In a real app, you'd add myButton to a view hierarchy.
        // For this console example, we simulate the action call.
        // [handler buttonTapped:myButton]; // Uncomment to simulate a tap immediately
    }
    return 0;
}

Accepting Text with UITextField

A UITextField allows users to enter single lines of text. It's perfect for names, passwords, or short messages.

  • It automatically brings up the keyboard when tapped.
  • You can configure its placeholder text, keyboard type (numeric, email, etc.), and security options (for passwords).
  • Use its text property to get the current input.

Handling UITextField Events

UITextField also uses the Target-Action pattern for various events. Common events include:

  • UIControlEventEditingChanged: The text content has changed.
  • UIControlEventEditingDidEndOnExit: The user pressed the "Return" key.
  • UIControlEventEditingDidBegin: The user started editing the text field.

You can also assign a delegate to a text field for more advanced control, like validating input or custom keyboard behavior.

Observing Text Input

This example shows how to set up a text field and react when its content changes. The console will log the text as it's "typed".

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface TextFieldHandler : NSObject
- (void)textFieldDidChange:(UITextField *)sender;
@end

@implementation TextFieldHandler
- (void)textFieldDidChange:(UITextField *)sender {
    NSLog(@"Text field content changed: %@", sender.text);
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        TextFieldHandler *handler = [[TextFieldHandler alloc] init];

        UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, 280, 40)];
        myTextField.placeholder = @"Type here...";
        myTextField.borderStyle = UITextBorderStyleRoundedRect;
        
        // Attach an action for when the text changes
        [myTextField addTarget:handler
                        action:@selector(textFieldDidChange:)
              forControlEvents:UIControlEventEditingChanged];

        NSLog(@"Text field created. Simulating input...");
        // Simulate some input changes
        myTextField.text = @"H";
        [handler textFieldDidChange:myTextField];
        myTextField.text = @"He";
        [handler textFieldDidChange:myTextField];
        myTextField.text = @"Hello";
        [handler textFieldDidChange:myTextField];
    }
    return 0;
}

Identify the UI Control

Which UIKit control is primarily used for displaying static, non-editable text, such as a title or a descriptive message?

Recap: Common Controls & Events

In this lesson, we explored fundamental UIKit controls and how to make them interactive:

  • UILabel: Displays static, read-only text.
  • UIButton: Triggers actions via taps, using the Target-Action pattern.
  • UITextField: Accepts single-line text input, also using Target-Action for changes.

These controls are the foundation for building user interfaces in Objective-C iOS apps. Understanding their properties and event handling is crucial for both new development and maintaining legacy code.

คำถามที่พบบ่อย

บทเรียน “ตัวควบคุมและเหตุการณ์ UI ทั่วไป” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวควบคุมและเหตุการณ์ UI ทั่วไป” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวควบคุมและเหตุการณ์ UI ทั่วไป”

ทำงานกับตัวควบคุม UIKit มาตรฐาน เช่น ปุ่ม ป้ายกำกับ และช่องข้อความ พร้อมเรียนรู้การจัดการการโต้ตอบและเหตุการณ์ของผู้ใช้ คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวควบคุมและเหตุการณ์ UI ทั่วไป” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม

ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. UIView และ UIViewController
  2. ตัวควบคุมและเหตุการณ์ UI ทั่วไป
  3. มุมมองตารางและมุมมองคอลเลกชัน
  4. Auto Layout ด้วย NSLayoutConstraint
← กลับไปที่ Objective-C iOS Development for Legacy & Enterprise Apps