如何向iPhone应用程序的UI添加自定义视图? [英] How do I add a custom view to iPhone app's UI?

查看:63
本文介绍了如何向iPhone应用程序的UI添加自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在致力于iPad的开发,但我仍在学习一切如何协同工作.我了解如何使用Xcode和Interface Builder将标准视图(即按钮,表视图,日期选择器等)添加到UI中,但是现在我试图在UISplitView的左侧窗口中添加自定义日历控件(TapkuLibrary).不涉及Interface Builder的应用程序,对不对?因此,如果我有一个自定义视图(在本例中为TKCalendarMonthView),如何以编程方式将其添加到UI中的一个视图(在本例中为RootViewController)?以下是我项目中的一些相关代码段...

I'm diving into iPad development and I'm still learning how everything works together. I understand how to add standard view (i.e. buttons, tableviews, datepicker, etc.) to my UI using both Xcode and Interface Builder, but now I'm trying to add a custom calendar control (TapkuLibrary) to the left window in my UISplitView application which doesn't involve Interface Builder, right? So if I have a custom view (in this case, the TKCalendarMonthView), how do I programmatically add it to one of the views in my UI (in this case, the RootViewController)? Below are some relevant code snippets from my project...

RootViewController接口

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {

    DetailViewController *detailViewController;

    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
}

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

- (void)insertNewObject:(id)sender;

TKCalendarMonthView界面

@class TKMonthGridView,TKCalendarDayView;
@protocol TKCalendarMonthViewDelegate, TKCalendarMonthViewDataSource;


@interface TKCalendarMonthView : UIView {

    id <TKCalendarMonthViewDelegate> delegate;
    id <TKCalendarMonthViewDataSource> dataSource;

    NSDate *currentMonth;
    NSDate *selectedMonth;
    NSMutableArray *deck;


    UIButton *left;
    NSString *monthYear;
    UIButton *right;

    UIImageView *shadow;
    UIScrollView *scrollView;


}
@property (readonly,nonatomic) NSString *monthYear;
@property (readonly,nonatomic) NSDate *monthDate;
@property (assign,nonatomic) id <TKCalendarMonthViewDataSource> dataSource;
@property (assign,nonatomic) id <TKCalendarMonthViewDelegate> delegate;

- (id) init;
- (void) reload;
- (void) selectDate:(NSDate *)date;

在此先感谢您的帮助!我还有很多东西要学习,因此,如果这个问题无论如何都是荒谬的,我深表歉意.我现在将继续研究这个问题!

Thanks in advance for all your help! I still have a ton to learn, so I apologize if the question is absurd in any way. I'm going to continue researching this question right now!

推荐答案

假设您已初始化自定义UIView,则需要将其添加为viewController视图的子视图.

Assuming you have initialized the custom UIView, you need to add it as a subview of the viewController's view.

-(void)addSubview:(UIView *)view

例如,如果您有一个名为myVC的普通viewController,它的视图只是空白的白色UIView,您会这样说:

So an example would be if you have a plain viewController called myVC, which has simply a blank white UIView as its view, you would say this:

CGRect customViewsFrame = CGRectMake(10, 30, 5, 2);
MyCustomView *myView = [[MyCustomView alloc] initWithFrame:customViewsFrame];
[[myVC view] addSubview:myView];
[myView release]; //Since you called "alloc" on this, you have to release it too

然后它将显示在viewController的视图中,并占用CGRect指示的空间.

Then it will show up in the viewController's view, taking up the space indicated by the CGRect.

CGRect的坐标在您要添加的超级视图的本地坐标系中指定位置(如果我没有记错的话).

The CGRect's coordinates specify a location in the local coordinate system of the superview you are adding to, if I'm not mistaken.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆