1

我肯定在这里做一些完全愚蠢的事情,希望有人能告诉我什么!总之,我想在单击按钮(我将使用分段按钮)时以编程方式切换子视图。它不需要动画。理想情况下,我会从笔尖加载这些视图的内容。

我正在将一个从笔尖初始化的 UIViewController 子类推到一个导航控制器上。此视图控制器及其“默认”视图显示正常。这个笔尖包含:

File's Owner [Class=my view controller subclass]
First Responder
View
--Bar Segmented Control
--View //this is the 'subview' whose content I want to toggle

也许我在我的 IB 网点上犯了一个错误?它们连接如下:

switchableView <--> View
view <--> View

并在哪里switchableView声明:

IBOutlet UIView *switchableView;
...
@property (nonatomic, retain)   UIView *switchableView;

我被困在替换这个 UIView 的内容上。首先,我只想在视图第一次出现时将其设置为默认值。一旦我得到了它,将它交换到其他东西应该很容易,因为我已经可以捕获分段按钮生成的事件。有人可以告诉我我做错了什么吗?

- (void)loadView {
NSLog(@"loadView invoked");
[super loadView];
UIView *view = [[UIView alloc] init];
UILabel *label = [[UILabel alloc] init];
label.text = @"Segment1";
[view addSubview:label];
[switchableView addSubview:view]; //this is what I can't work out
[view release];
[label release];    
}

该方法调用(原样viewDidLoad),但都没有帮助。我想我[switchableView addSubview:view]做错了。我也尝试过switchableView = view,但是在 Java 和 Perl 的安全性方面有很长一段时间让我不能 100% 确定尝试是否正确!

这里希望有人可以让我直截了当。提前致谢,

保罗

4

4 回答 4

3

在检查了 UICatalog 示例代码后最终解决了这个问题(感谢你们给我一些指示)。以下作品:

- (void)loadView {
    [super loadView];

    //define what will go in the seg1 view
    UILabel *mylabel = [[UILabel alloc] initWithFrame:switchableView.frame];
    mylabel.text=@"finally";
    self.seg1view = [[[UIView alloc] initWithFrame:switchableView.frame] autorelease];
    [self.seg1view addSubview:mylabel];
    [mylabel release];

    //define what will go in the seg2 view
    UILabel *mylabel2 = [[UILabel alloc] initWithFrame:switchableView.frame];
    mylabel2.text=@"it works!";
    self.seg2view = [[[UIView alloc] initWithFrame:switchableView.frame] autorelease];
    [self.seg2view addSubview:mylabel2];
    [mylabel2 release]; 

    //default the switchable view to seg1view
    [switchableView addSubview:self.seg1view];
}

-(void)doTheSwitch {
    if (segmentedButton.selectedSegmentIndex == 0) {
        [seg2view removeFromSuperview];
        [switchableView addSubview:seg1view];
    }
    if (segmentedButton.selectedSegmentIndex == 1) {    
        [seg1view removeFromSuperview];
        [switchableView addSubview:seg2view];
    }   
}

其中以下也被定义为属性(保留,非原子)

IBOutlet UIView *switchableView;
UIView  *seg1view;
UIView  *seg2view;
于 2009-07-22T18:33:50.943 回答
0

这里有一些片段可以帮助你。第一个只是导航控制器的初始化

// In AppDelegate.m
-(void)applicationDidFinishLaunching:(UIApplication *)application
{
  // Create and configure the main view controller.
  UIViewController *firstViewController = [[UIViewController alloc] init];

  // Add create and configure the navigation controller.
  UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
  self.navController = navigationController;
  [navigationController release];

  // Configure and display the window.
  [window addSubview:navController.view];
  [window makeKeyAndVisible];
}

然后,当您准备好使用新视图时,只需将其推到那里即可。

// Wherever you need to get your new view on there
UIViewController *secondViewController = [[UIViewController alloc] init];
[[self navigationController] pushViewController:secondViewController animated:YES];
[secondViewController release];

你真的应该每天完成大约 5 个 Apple 示例项目,才能真正熟练掌握 iPhone 世界。首先看看这个:TableSearch

于 2009-07-22T00:33:51.733 回答
0

最可能的问题是您没有在视图上设置框架,这意味着当它们被成功附加时,它们的大小为 0x0。

尝试类似:

- (void)loadView {
  NSLog(@"loadView invoked");
  [super loadView];

  CGRect labelFrame = CGRectZero;
  labelFrame.size.height = switchableView.size.height;
  labelFrame.size.width = switchableView.size.width;

  UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
  label.text = @"Segment1";
  [switchableView addSubview:label]; //this is what I can't work out
  [label release];    
}

还删除了视图,因为它是无偿的(一个 UILabel 是一个 UIView,您正在堆叠三个视图,其中 2 个只有一个覆盖其全帧的子视图。)并且它省略了 UIViewController 视图属性(通常它是不好的使局部变量与实例变量或属性同名,您可以打开编译器警告以避免这种情况)。

于 2009-07-22T07:09:06.257 回答
0

如果我正确理解了您的问题,那么您正在尝试将一种观点与另一种观点交换。实现此目的的一种方法如下:

将 UIView 添加到名为 switchView 的根视图。在代码或 Interface Builder 中创建一个名为 subView1 的新 UIView 并在根视图加载时将其添加为 switchView 的子类:

[switchView addSubview:subView1];

当用户在根视图中切换分段控件时,以类似的方式创建一个名为 subView2 的新 UIView 并使用 switchView 的当前子视图切换它:

[subView1 removeFromSuperview];
[switchView addSubview:subView2];
于 2009-07-22T07:19:37.080 回答