insertSubview:AtIndex:不起作用

时间:2010-12-19 10:49:46

标签: iphone objective-c subview

我查看了有关此问题的材料,但仍然无法完全弄明白。 主要问题是我正在使用此方法,但视图未显示... 该应用程序是由两个视图构建的 - 主要视图(我将其添加为主要笔尖的子视图) - 有一个文本字段,我写了一些文字。 有一个按钮,当按下该按钮时,action方法会加载第二个视图,该视图的标签中包含一些文本。 第一个viewController的源代码:

#import <UIKit/UIKit.h>

@class TxtRunnerViewController;

@interface Itai_s_text_app_take_2ViewController : UIViewController {
 int sliderSpeed;
 IBOutlet UITextField  *textInput;
 TxtRunnerViewController *trvc;
}

@property (nonatomic, retain) IBOutlet UITextField *textInput;
@property (retain, nonatomic) TxtRunnerViewController *trvc;

- (IBAction)sliderChanged:(id)sender;//speed of text show changed

- (IBAction)textFieldDoneEditing:(id)sender;//dor 'DONE' on keyboard
- (IBAction)backgroundTap:(id)sender;//for handling tapping on background

- (IBAction)textEmButtonPressed:(id)sender;

@end

第一个ViewController的.m:     #import“Itai_s_text_app_take_2ViewController.h”     #import“TxtRunnerViewController.h”

@implementation Itai_s_text_app_take_2ViewController
@synthesize textInput;
@synthesize trvc;


- (IBAction)sliderChanged:(id)sender
{
 UISlider *slider = (UISlider *)sender;
 sliderSpeed =  (int)(slider.value + 0.5f);//setting the speed determinned by the usr in slider
 NSLog(@"Slider value is %@", sliderSpeed);
}

- (IBAction)textFieldDoneEditing:(id)sender
{
    [sender resignFirstResponder];
 NSLog(@"our text input  is %@", textInput.text);
}

- (IBAction)backgroundTap:(id)sender
{
 [textInput resignFirstResponder]; 
}

- (IBAction)textEmButtonPressed:(id)sender
{
 NSLog(@"button pressed");
 if ([textInput.text length]==0) 
 {
  NSString *msg = nil;
  msg = @"Write text to transmit";
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Forgot something?"
              message:msg
                delegate:self
             cancelButtonTitle:@"Back" 
             otherButtonTitles:nil];

  [alert show]; 
  [alert release]; 
  [msg release];
 }
 else { //init 
  NSLog(@"HI!");
  if (self.trvc == nil)
  {
   NSLog(@"if accepted");
   TxtRunnerViewController *tr = [[TxtRunnerViewController alloc] initWithNibName:@"TxtRunner"
                       bundle:nil];
   self.trvc = tr;
   [tr release];
  }
  [self.view removeFromSuperview];
  [self.view insertSubview:trvc.view atIndex:0];
  NSLog(@"InsertAtIndex was operated...!");
  //[self.view addSubview:tvc.view];
 }

}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
 if(self.trvc.view.superview == nil)
  self.trvc = nil;
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
 [trvc release];
    [super dealloc];
}

第二个视图只是空的(在它的nib文件中添加了一个标签):     #import

@interface TxtRunnerViewController : UIViewController {

}

@end

.m文件是默认文件,没有添加任何代码。 当我在第一个视图中按下按钮时 - 它的视图消失但第二个视图没有出现,只显示空白的白色视图,没有任何内容。

Doe的

2 个答案:

答案 0 :(得分:0)

该行: [self.view removeFromSuperview];

从超级

中删除视图 然后是电话

[self.view insertSubview:trvc.view atIndex:0];

向其添加trvc - 但该视图已被删除且不再可见。

您需要将trvc添加到superview。

答案 1 :(得分:0)

如果你按下TxtRunnerViewController,它将完全隐藏第一个,但它仍然存在于后台。 (没有removeFromSuperview)

TxtRunnerViewController *vw = [[TxtRunnerViewController alloc]initWithNibName:@"TxtRunner" bundle:nil];
[self.view pushViewController:vw.view animated:NO];
[vw release];

要删除第一个视图,这有点困难。您需要在RootViewController中实现一个委托,该委托将在单击第一个视图的完成按钮后触发。然后在RootViewController中执行一些代码,显示第二个视图并删除第一个视图。

对于委托样本,在创建新项目时打开Utility Application Sample并弄清楚它是如何工作的或在线搜索一些示例。