UIView وUIViewController
افهموا دورة حياة UIView وUIViewController ومسؤولياتهما، فهما لبنات بناء واجهات iOS.
UIView وUIViewController درس مجاني في Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Objective-C iOS Development for Legacy & Enterprise Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Introduction to iOS UI
Welcome to building iOS interfaces! Every visual element you see in an iOS app, from buttons to entire screens, is built using two core components: UIView and UIViewController.
These are the fundamental building blocks that allow you to display content and manage user interactions in your Objective-C applications.
What is a UIView?
A UIView is the basic rectangular region that draws content and handles events on the screen. Think of it as a canvas where you can draw shapes, display text, or show images.
- It's the base class for all visual elements.
- It defines the content's position, size, and appearance.
- It can contain other views, creating a view hierarchy.
Basic UIView Properties
UIViews have important properties that control their appearance and layout. Here are a few common ones:
frame: Defines the view's size and position relative to its superview.bounds: Defines the view's size and position relative to its own coordinate system.backgroundColor: Sets the view's background color.alpha: Controls the view's transparency (0.0 is fully transparent, 1.0 is fully opaque).
Creating a UIView
You can create and configure a UIView programmatically. Here's how you might set up a simple red square view:
// In an Objective-C file (e.g., within a method)
CGRect viewFrame = CGRectMake(50, 50, 100, 100);
UIView *myView = [[UIView alloc] initWithFrame:viewFrame];
myView.backgroundColor = [UIColor redColor];
myView.alpha = 0.7;
// myView is now ready to be added to another viewWhat is a UIViewController?
A UIViewController is a crucial component that manages a hierarchy of views (often just one main view) and coordinates the flow of information between the views and your app's data model.
It acts as the brain for a specific screen or section of your app, handling user input, updating the view, and responding to system events.
UIViewController's Main View
Every UIViewController has a primary view property, which is an instance of UIView. This view acts as the root of the view hierarchy that the view controller manages.
You typically add all other UI elements (like buttons, labels, etc.) as subviews to this main view.
The `viewDidLoad` Lifecycle Method
UIViewControllers have a lifecycle with methods that are called at different stages. One of the most important is viewDidLoad.
- It's called once after the controller's view has been loaded into memory.
- This is the perfect place to perform initial setup for your views, like adding subviews, setting up constraints, or loading initial data.
- It's called only once during the lifetime of the view controller.
Implementing `viewDidLoad`
Here's how you might override viewDidLoad in your custom view controller to add some initial UI setup:
// MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad]; // Always call super!
self.view.backgroundColor = [UIColor lightGrayColor];
UILabel *welcomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
welcomeLabel.text = @"Hello from MyViewController!";
welcomeLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:welcomeLabel];
}
@end`viewWillAppear` and `viewDidAppear`
Besides viewDidLoad, two other common lifecycle methods relate to a view appearing on screen:
viewWillAppear:: Called just before the view controller's view is added to the view hierarchy and about to be displayed. Good for updating UI that might change frequently.viewDidAppear:: Called after the view controller's view has been fully presented on screen. Good for starting animations or fetching data that requires the view to be visible.
Quick Check: UI Components
Which of the following statements best describes the primary responsibility of a UIViewController?
Recap: UIView & UIViewController
You've learned about the fundamental building blocks of iOS user interfaces!
UIViewis the basic rectangular area for drawing and event handling.UIViewControllermanages a view hierarchy and coordinates the interaction between views and data.- The
viewproperty of aUIViewControlleris the root of its managed UI. - Lifecycle methods like
viewDidLoad,viewWillAppear, andviewDidAppearare key for setting up and updating your UI at different stages.
Next, we'll explore common UI controls and how to handle user events!
الأسئلة الشائعة
هل درس «UIView وUIViewController» مجاني؟
نعم — نص درس «UIView وUIViewController» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Objective-C iOS Development for Legacy & Enterprise Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Objective-C iOS Development for Legacy & Enterprise Apps 4 دروس في المجموع.
ماذا ستتعلم في «UIView وUIViewController»؟
افهموا دورة حياة UIView وUIViewController ومسؤولياتهما، فهما لبنات بناء واجهات iOS. تتمرن على Objective-C iOS Development for Legacy & Enterprise Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Objective-C iOS Development for Legacy & Enterprise Apps؟
لا تُشترط خبرة سابقة. Objective-C iOS Development for Legacy & Enterprise Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «UIView وUIViewController»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Objective-C iOS Development for Legacy & Enterprise Apps هذا؟
نعم. كل درس في Objective-C iOS Development for Legacy & Enterprise Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- UIView وUIViewController
- عناصر تحكم UI والأحداث الشائعة
- عروض الجداول وعروض المجموعات
- التخطيط التلقائي باستخدام NSLayoutConstraint