【Cocoa】 Initializing View Instances Created in Interface Builder

Initializing View Instances Created in Interface Builder

View instances that are created in Interface Builder don‘t call initWithFrame: when their nib files are loaded, which often causes confusion. Remember that Interface Builder archives an object when it saves a nib file, so the view instance will already have been created and initWithFrame: will already have been called.

The awakeFromNib method provides an opportunity to provide initialization of a view when it is created as a result of a nib file being loaded. When a nib file that contains a view object is loaded, each view instance receives an awakeFromNib message when all the objects have been unarchived. This provides the object an opportunity to initialize any attributes that are not archived with the object in Interface Builder. The DraggableItemView class is extremely simple, and doesn‘t implementawakeFromNib.

There are two exceptions to the initWithFrame: behavior when creating view instances in Interface Builder. Its important to understand these exceptions to ensure that your views initialize properly.

If you have not created an Interface Builder palette for your custom view, there are two techniques you can use to create instances of your subclass within Interface Builder. The first is using the Custom View proxy item in the Interface Builder containers palette. This view is a stand-in for your custom view, allowing you to position and size the view relative to other views. You then specify the subclass of NSView that the view represents using the inspector. When the nib file is loaded by the application, the custom view proxy creates a new instance of the specified view subclass and initializes it using the initWithFrame: method, passing along any autoresizing flags as necessary. The view instance then receives an awakeFromNib message.

The second technique applies when your custom subclass inherits from a view that Interface Builder provides direct support for. To take advantage of Interface Builder’s built-in support, create an instance of the view that Interface Builder has direct support for, and then use the inspector to change the class name to your custom subclass. For example, you can create an NSScrollView instance in Interface Builder and specify that a custom subclass (MyScrollView) should be used instead, again using the inspector. In this case, when the nib file is loaded by the application, the view instance has already been created and the MyScrollView implementation of initWithFrame: is never called. The MyScrollView instance receives an awakeFromNibmessage and can configure itself accordingly.

时间: 2024-10-12 12:04:07

【Cocoa】 Initializing View Instances Created in Interface Builder的相关文章

【Android】自定义View —— 滑动的次数选择器

[关键词] 自定义View 次数选择器 滑动 [问题] 实现一个可滑动的次数选择器: [效果图] 「原型图」 「实现图」 [分析] 对外提供简单的Change监听接口: 如果处于两者之间就需要做判断:大于一半就自动跳转到下一个,小于一半,则回到上一个: 通过Scroller及其startScroll()方法来实现回弹效果: 要灵活控制刻度的最小值和最大值,因为可能随着需求的更改,这个值很容易发生改变(即处理好对应关系): 在自定义View中只画刻度和文字,至于红色的指针和外面的透明渐变图层则可以

【Exception】 java.lang.NoSuchMethodError: android.app.AlertDialog$Builder.setOnDismissListener

f(Build.VERSION.SDK_INT >10) builder =newAlertDialog.Builder(getActivity(), R.style.Theme.Sherlock.Dialog);else builder =newAlertDialog.Builder(getActivity());AlertDialog.Builder builder;try{ builder =newAlertDialog.Builder(getActivity(), R.style.The

【Android】自定义View、画布Canvas与画笔Paint

安卓自定义View其实很简单.这个View可以像<[Android]利用Java代码布局,按钮添加点击事件>(点击打开链接)一样,利用Java代码生成一系列的组件.也可以配合画布Canvas与画笔Paint来使用. 下面用一个例子来说明.如下图,有一个自定义布局View,里面摆放着,利用画布Canvas与画笔Paint绘制出来的蓝色正方形与红色文字. 在res\layout\activity_main.xml中,直接像摆放安卓固有组件一样,可以直接使用这个我定义组件.里面有蓝色正方形与红色文字

【android】把view保存为图片的方法以及解决保存后图片背景变黑色的问题

上代码: public class MainActivity extends Activity { ImageView imgView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgView = (ImageView) findViewById(R.id.im

【转载】自定义View学习笔记之详解onMeasure

网上对自定义View总结的文章都很多,但是自己还是写一篇,好记性不如多敲字! 其实自定义View就是三大流程,onMeasure.onLayout.onDraw.看名字就知道,onMeasure是用来测量,onLayout布局,onDraw进行绘制. 那么何时开始进行View的绘制流程,这就要从ViewRoot和DecorView的概念说起. ViewRoot对应于ViewRootImpl类,是连接WindowManager和DecorView的纽带,View的三大绘制流程都是通过ViewRoo

【学习】自定义view

自定义控件其实很简单1/2   Canvas的使用 自定义控件其实很简单1/3   Shader与画布的旋转 自定义控件其实很简单2/3   view的测量 自定义控件其实很简单1/4   FontMetrics意为字体测量   绘制路劲 自定义控件其实很简单3/4 自定义控件其实很简单1/6 自定义控件其实很简单1/12 自定义控件其实很简单5/12 自定义控件其实很简单7/12

【Android】自定义View

Android自定义View(控件): 学员资料:Android手写文字(涂鸦)实现 Android 自定义View (一) Android自定义View的实现方法,带你一步步深入了解View(四) [原] Android 自定义View步骤 Android中自定义属性与格式标签详解

【转】设计模式(三)建造者模式Builder(创建型)

(http://blog.csdn.net/hguisu/article/details/7518060) 1. 概述 在软件开发的过程中,当遇到一个"复杂的对象"的创建工作,该对象由一定各个部分的子对象用一定的算法构成,由于需求的变化,复杂对象的各个部分经常面临剧烈的变化,但将它们组合在一起的算法相对稳定. 例子1:买肯德基 典型的儿童餐包括一个主食,一个辅食,一杯饮料和一个玩具(例如汉堡.炸鸡.可乐和玩具车).这些在不同的儿童餐中可以是不同的,但是组合成儿童餐的过程是相同的.   

【温故知新】c#抽象类abstract与接口interface

1.什么是抽象类 先来看MSDN对抽象类描述: 抽象类是一些留有部分或全部成员未实现的类,以便可以由派生类来提供实现. 在面向对象的编程中,抽象类用作层次结构的基类,并表示不同对象类型组的通用功能. 正如名称“抽象”所暗指的,抽象类通常不会直接与问题域中的具体实体对应. 不过,抽象类会表示多个不同的具体实体之间的共同之处. 仅当存在已声明但未定义的抽象方法时,才会将类视为抽象类. 因此,具有抽象方法的类并不一定是抽象类. 除非类具有未定义的抽象方法,否则不要使用 AbstractClass 特性