多个UIViewController的困境

问题描述:

我比较新,开始使用我的第二个应用程序,并且对多个视图控制器感到苦恼。我已将相关代码添加到此电子邮件的底部。这是发生了什么:多个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 
+0

是否在您的roomviewcontroller中启用了交互功能? – Daniel 2009-07-31 13:30:39

明白了。我现在觉得自己有点傻。这是一个双重问题。

1)我把clipsToBounds ...结果我的RoomVC是不正确的大小,所以项目不在活动区域​​。但为什么它不是正确的尺寸?

2)在XIB中,我的RoomViewController应该被自动化到左上角。相反,所有6个Autosize箭头都打开了,所以它将它压扁成奇怪的尺寸。

我知道没有其他人可以通过我的代码想出来,所以祝你有美好的一天。

-k。