嗯,我不確定,但我想我找到了解決方案的開始。這是我的想法。
我認爲面向IB的設計。 編譯器會要求IB實例化視圖。 但是,如果我們創建了一個UITableViewController子類,那麼我們所有的方法都會引用這個視圖的實例化(正確的單詞?)。
因此,爲了避免這種衝突,我們可以在.M中刪除引用表的初始化的代碼:initWithStyle和Table源的編譯標記。我們只是讓任何視圖和委託所需的View生命週期。
我發現了一些使用它的例子。這裏是設計與靜態細胞對IB的詳細視圖表的.M:
#import "PictureListDetail.h"
@implementation PictureListDetail
@synthesize managedObjectContext;
@synthesize currentPicture;
@synthesize titleField, descriptionField, imageField;
@synthesize imagePicker;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// If we are editing an existing picture, then put the details from Core Data into the text fields for displaying
if (currentPicture)
{
[titleField setText:[currentPicture title]];
[descriptionField setText:[currentPicture desc]];
if ([currentPicture smallPicture])
[imageField setImage:[UIImage imageWithData:[currentPicture smallPicture]]];
}
}
#pragma mark - Button actions
- (IBAction)editSaveButtonPressed:(id)sender
{
// If we are adding a new picture (because we didnt pass one from the table) then create an entry
if (!currentPicture)
self.currentPicture = (Pictures *)[NSEntityDescription insertNewObjectForEntityForName:@"Pictures" inManagedObjectContext:self.managedObjectContext];
// For both new and existing pictures, fill in the details from the form
[self.currentPicture setTitle:[titleField text]];
[self.currentPicture setDesc:[descriptionField text]];
if (imageField.image)
{
// Resize and save a smaller version for the table
float resize = 74.0;
float actualWidth = imageField.image.size.width;
float actualHeight = imageField.image.size.height;
float divBy, newWidth, newHeight;
if (actualWidth > actualHeight) {
divBy = (actualWidth/resize);
newWidth = resize;
newHeight = (actualHeight/divBy);
} else {
divBy = (actualHeight/resize);
newWidth = (actualWidth/divBy);
newHeight = resize;
}
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
UIGraphicsBeginImageContext(rect.size);
[imageField.image drawInRect:rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save the small image version
NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0);
[self.currentPicture setSmallPicture:smallImageData];
}
// Commit item to core data
NSError *error;
if (![self.managedObjectContext save:&error])
NSLog(@"Failed to add new picture with error: %@", [error domain]);
// Automatically pop to previous view now we're done adding
[self.navigationController popViewControllerAnimated:YES];
}
// Pick an image from album
- (IBAction)imageFromAlbum:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
}
// Take an image with camera
- (IBAction)imageFromCamera:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self presentViewController:imagePicker animated:YES completion:nil];
}
// Resign the keyboard after Done is pressed when editing text fields
- (IBAction)resignKeyboard:(id)sender
{
[sender resignFirstResponder];
}
@end
可以在這裏找到:enter link description here
你覺得呢?
這是我擁有的: - (id)initWithStyle:(UITableViewStyle)風格 { self = [super initWithStyle:style]; if(self){ //定製初始化 } return self; } 然後,我通過引入一個UITextField和一個頭文件來保持IB的設計,但是我沒有將UITextField作爲IBOutlet鏈接到.h btw,如何引入代碼?它不是那麼明確.. – louisD