多个UIViewController问题

时间:2009-07-31 11:59:03

标签: iphone objective-c cocoa-touch uiviewcontroller

我比较新,开始使用我的第二个应用程序,并且使用多个视图控制器时遇到了很大的麻烦。我已将相关的代码位添加到此电子邮件的底部。这是正在发生的事情:

MainViewController:创建RoomViewController,然后询问其视图 RoomViewController:设置房间里面的物品(在这种情况下,Hector和咖啡)。 Room.m:房间的背景 Item.m:获取所有信息,创建自己,启用用户交互。 (touchesBegan等)

问题是: 在RoomViewController中创建项目时,触摸不起作用。 当在MainViewController中创建项目时,触摸工作(即使创建为在roomView内进行)。

我需要将某种火炬传递给RoomViewController以告诉它接受互动吗? 我一直在把桌子撞到桌子上扔东西,因为我确信它一定很简单,但我花了好几个小时试图把它钉死。 你能提供的任何帮助都会让我非常开心。

-k。

Main view controller:

- (void)viewDidLoad {
    RoomViewController *viewController = [[RoomViewController alloc] initWithNibName:@"RoomViewController" bundle:nil];
    viewController.mainViewController = self;
    self.roomViewController = viewController;
    [viewController release];
    roomView = self.roomViewController.view;
    [self.view addSubview:self.roomViewController.view];
}

房间视图控制器:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        room = [[Room alloc] initRoom:@"OFFICE" displayName:@"Office"];
        [self.view addSubview:room];

        NSString *itemName = @"COFFEE";
        CGPoint iLocation = CGPointMake(82, 192);
        CGPoint hLocation = CGPointMake(120, 200);
        NSString *hFace = @"FL";

        Item *newItem = [[Item alloc] initItem:itemName viewController:self atLocation:iLocation];
        [self.view addSubview:newItem];

        [newItem release];

        [self.view addSubview:hector];
        hector.center = CGPointMake(350, 200);      
    }
    return self;
}

Room.m:

-(id)initRoom:(NSString *)rName displayName:(NSString *)rDisplay {
    roomName = rName;
    displayName = rDisplay;
    NSString *bgFile = [NSString stringWithFormat:@"%@_BG.png", roomName];
    [self initWithImage:[UIImage imageNamed:bgFile]];
    [self setCenter:CGPointMake(240, 135)];
    return self;
}

Item.m:

- (id)initItem:(NSString *)iName viewController:(RoomViewController *)vc atLocation:(CGPoint)iLocation {

    itemName = iName;

    NSString *itemFile = [NSString stringWithFormat:@"%@_ITEM.png", itemName];
    [self initWithImage:[UIImage imageNamed:itemFile]];
    [self setCenter:iLocation];
    myViewController = vc;
    self.userInteractionEnabled = YES;

    return self;
}   

-(void)touchesBegan... etc

1 个答案:

答案 0 :(得分:1)

知道了。我觉得现在有点傻瓜。这是一个双重问题。

1)我把clipsToBounds打开...结果我的RoomVC尺寸不合适,所以这些物品不在活动区域​​。但为什么不是合适的尺寸?

2)在XIB中,我的RoomViewController应该已经自动调整到左上角。相反,所有6个自动调整大小的箭头都打开了,因此它将它压缩成时髦的尺寸。

我知道其他任何人都无法通过我的代码解决这个问题,所以,祝你有个美好的一天。

-k。