如何在iPhone应用程序的UI中添加自定义视图?

时间:2010-05-12 23:26:36

标签: iphone xcode uisplitviewcontroller ipad

我正在深入研究iPad开发,我仍然在学习一切如何协同工作。我理解如何使用Xcode和Interface Builder将标准视图(即按钮,tableviews,datepicker等)添加到我的UI,但现在我正在尝试在我的UISplitView中向左窗口添加自定义日历控件(TapkuLibrary)不涉及Interface Builder的应用程序,对吧?因此,如果我有自定义视图(在本例中为TKCalendarMonthView),我如何以编程方式将其添加到我的UI中的一个视图(在本例中为RootViewController)?以下是我项目中的一些相关代码片段......

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;

提前感谢您的帮助!我还有很多需要学习的东西,所以如果这个问题在任何方面都是荒谬的,我会道歉。我现在要继续研究这个问题!

2 个答案:

答案 0 :(得分:3)

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

- (void)addSubview:(UIView *)view

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

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指示的空格。

如果我没有弄错的话,CGRect的坐标指定要添加到的超级视图的本地坐标系中的位置

CGRect CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height);

答案 1 :(得分:1)

我没有启动进入Mac OS X,所以我无法完全验证这一点,但这是您的一般模式:

RootViewController.h:

...
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>
{
    ...
    TKCalendarMonthView* calendarView;
    ...
}
...
@property (nonatomic, retain) TKCalendarMonthView* calendarView;
...

RootViewController.m:

...
@synthesize calendarView;
...
- (void)dealloc
{
    ...
    [calendarView release];
    ...
}
...
- (void)viewDidLoad
{
    ...
    TKCalendarMonthView* aCalendarView = [[TKCalendarMonthView alloc] init]; // <-- possibly initWithFrame here
    self.calendarView = aCalendarView;
    [aCalendarView release];
    [self addSubview:self.calendarView];
    ...
}