0

这是我在 SO 上的第一篇文章,所以你好!

我也是 Xcode 和 Obj-C 的新手,所以不要太苛刻。

我正在关注斯坦福大学 youtube.com/watch?v=L-FK1TrpUng 的演示,由于某种原因,我遇到了错误。与其从头开始,我更愿意找出我哪里出错了。

好的,就这样吧。

我有两个视图控制器,我目前正在学习推送和弹出。

我的第一个视图控制器 (firstViewController.h) 标头:

    #import <UIKit/UIKit.h>       
@interface FirstViewController : UIViewController {
    }
     - (IBAction)pushViewController:(id)sender;
    @end

然后最初这是在实现文件(firstViewController.m)中设置的,像这样

#import "firstViewController.h"
    @implementation FirstViewController
    - (IBAction)pushViewController:(id)sender{
}

此时使用 IB 我 ctrl 从“文件所有者”拖到“UIButton”并连接“pushViewController”

但是,在途中的某个地方,我收到了某种错误,我忽略了它。

现在我将第二个视图控制器添加到我的 firstViewController.m 中,如下所示;

#import "firstViewController.h"
#import "secondViewController.h"

@implementation FirstViewController

    - (IBAction)pushViewController:(id)sender{
     SecondViewController *secondViewController = [[SecondViewController alloc] init];
     secondViewController.title = @"Second"; 
     [self.navigationController pushViewController:secondViewController animated:YES];
     [secondViewController release];
    }

我之前收到的错误似乎以某种方式阻止了我从我的 secondViewController 笔尖中的 textLabel 拖动 ctrl

(secondeViewController.h)

#import "firstViewController.h"
#import "secondViewController.h"


@implementation FirstViewController
- (IBAction)pushViewController:(id)sender{
 SecondViewController *secondViewController = [[SecondViewController alloc] init];
 secondViewController.title = @"Second";
 [self.navigationController pushViewController:secondViewController animated:YES];
 [secondViewController release];
}

所以我通过在 firstViewController.xib 中右键单击它从我的原始 UIButton 中删除了引用。

现在我无法重新创建从“文件所有者”到“UIButtons”、“pushViewController”插座的链接(它是一个插座还是一个动作?),也不能在我的 secondViewControllers 笔尖中创建从“文件所有者”到“UILabel”的链接'。

有什么帮助吗?

如果有人感兴趣,这里的项目文件。http://zer-o-one.com/upload/files/PushPop.zip

非常感激。

4

1 回答 1

1

出口是将一个对象连接到另一个对象的路径。动作是在特定对象上调用以响应事件的方法名称。Cocoa 传统上大量使用目标/动作进行通信,尽管现在这部分被块所取代。

无论如何,你的项目:

firstViewController.xib 错误地认为其文件所有者是“firstViewController”类型的类。它实际上是“FirstViewController”类型——像大多数编程语言一样,Objective-C 对类名是区分大小写的。在 Interface Builder 中,打开 firstViewController.xib,选择“File's Owner”,打开检查器并转到“i”选项卡,然后更正顶部的类名。完成后,尝试切换到连接选项卡(箭头指向右侧的选项卡),您应该会看到它已正确找到您的课程和 IBAction。然后,您应该能够控制拖动。

基本上相同的评论适用于 secondViewController。

如果您好奇,Objective-C 与例如 C++ 的不同之处在于,所有类名在运行时都是已知的,并且可以从其名称的字符串版本实例化一个类。这就是 XIB/NIB 的加载方式。

于 2010-10-12T16:48:18.583 回答