【问题标题】:Conflict between Interface builder and .m@.h : who win?Interface builder 和 .m@.h 之间的冲突:谁赢了?
【发布时间】:2012-06-06 13:16:22
【问题描述】:

我一直想知道 Xcode 如何解释 IB 和编程编码对象的使用。 例如:我在 .m 中初始化了一个表,但我在我正在处理的 tableviewcontroller 的属性中设置了简单的样式。所以?我注意到代码通过了 IB。

当我必须通过在第一个单元格中插入标题和 UITextField 来自定义详细信息表时,它首先出现,这在 IB 中非常容易。但是当我运行应用程序时,除了一个普通表格的模板之外什么都没有。 咕咕咕??

感谢您的帮助。 干杯, 路易斯

编辑

这里是 TableViewController 的实例:

    - (id)initWithStyle:(UITableViewStyle)style
    {
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

【问题讨论】:

    标签: ios interface-builder xcode4.2 uitextfield


    【解决方案1】:

    他们不应该越过。如果您使用 [[UITableView alloc] initWithStyle:UITableViewStyleGrouped] 实例化一个表,它会创建一个新表而无需通过 NIB。如果您从 NIB 实例化一个表,它会使用 -initWithCoder: 创建一个实例。


    更新后添加

    好的,你正在继承 UITableView。除了覆盖-initWithStyle:,您还需要覆盖-initWithCoder:-awakeFromNib

    从 NIB 加载自定义 UIView 的基本流程。

    • -initWithCoder: 用于实例化对象
    • 所有 NIB 连接都已建立(IBOutlets 和 IBAction 已连接)。
    • -awakeFromNib 被发送到对象

    这意味着如果您在-initWithCoder: 中设置一个值,则NIB 设置将获胜;如果您在-awakeFromNib 中设置一个值,您的代码将获胜。

    请务必阅读UIView 的子类化注释和覆盖方法部分。

    【讨论】:

    • 我不明白。实例化过程如何工作?结论是我必须在 ib 或 .h@.m 上设置一次?
    • 如果您在 IB 中创建表格视图,则不得为该表格视图调用 -initWithStyle:。如果您有特定问题,请发布代码示例。
    • 感谢您的回答@jefferyThomas,我在主要问题中编辑了代码。
    【解决方案2】:

    这完全取决于您如何初始化对象。如果您从控制器的 init 方法加载 UITableView 并调用 initWithStyle,这就是您的 UITableView 的样子。如果您想使用 IB,您需要使用 initWithNibName 初始化您的控制器,并与您的视图建立一个 IBOutlet 连接,该视图具有普通设置。

    【讨论】:

    • 这是我所拥有的:- (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // 自定义初始化 } return self;然后,我通过引入 UITextField 和标题继续使用 IB 进行设计,但是尽管我将 UITextField 作为 IBOutlet 链接到 .h 顺便说一句,但什么也没出现,如何引入代码?它不是那么明确..
    【解决方案3】:

    好吧,我不确定,但我想我找到了解决方案的开始。这是我的想法。

    我认为是面向 IB 的设计。 编译器将要求 IB 实例化视图。 但是如果我们创建了一个 UITableViewController 子类,那么我们就拥有了引用该视图的实例化(正确的词?)的所有方法。

    所以,为了避免这种冲突,我们可以删除.M中的代码,它指的是表的初始化:initWithStyle和关于表源的pragma标记。我们只是让任何视图和委托所需的视图生命周期。

    我发现了一些使用这个的例子。这是一个详细视图表的 .m,该表是用 IB 上的静态单元格设计的:

        #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

    你觉得呢??

    【讨论】:

      【解决方案4】:

      即使我现在独自在这篇文章中,我还是想发布答案!

      我终于弄清楚为什么我的文本字段没有出现,这表明谁在使用编译器。

      我实际上用一个实现方法的类对细节进行了子类化,用 coreData 填充表源。然后,假设的静态单元格实际上是用这些方法填充的。

      在我看来,这表明 .m 超过了 IB。

      【讨论】:

        猜你喜欢
        • 2011-04-27
        • 1970-01-01
        • 1970-01-01
        • 2011-03-14
        • 2012-06-02
        • 1970-01-01
        • 2014-06-05
        相关资源
        最近更新 更多