How do I add a custom view to iPhone app's UI?

2019-08-22 03:23发布

问题:

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

@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 interface

@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!

回答1:

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

- (void)addSubview:(UIView *)view

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

Then it will show up in the viewController's view, taking up the space indicated by the 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.

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



回答2:

I'm not booted into Mac OS X so I can't verify this completely, but this is your general pattern:

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];
    ...
}