【问题标题】:How do I add a custom view to iPhone app's UI?如何将自定义视图添加到 iPhone 应用程序的 UI?
【发布时间】:2010-05-12 23:26:35
【问题描述】:

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

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;

提前感谢您的帮助!我还有很多东西要学,所以如果这个问题有任何荒谬之处,我深表歉意。我现在要继续研究这个问题!

【问题讨论】:

    标签: iphone xcode uisplitviewcontroller ipad


    【解决方案1】:

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

    - (void)addSubview:(UIView *)view

    举个例子,如果你有一个名为myVC 的普通viewController,它的视图只是一个空白的白色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);

    【讨论】:

    • 谢谢,克里斯!这就说得通了。如何在父视图中定位它?这是我在自定义视图初始化期间所做的事情吗?
    【解决方案2】:

    我没有启动到 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];
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多