insertSubview:AtIndex:不起作用

发布于 2024-10-08 00:24:19 字数 3921 浏览 1 评论 0原文

我浏览了有关这个问题的材料,但仍然无法完全弄清楚。 主要问题是我正在使用这种方法,但视图没有显示...... 该应用程序由两个视图构建 - 主视图(我将其作为子视图添加到主笔尖) - 有一个文本字段,我可以在其中写一些文本。 有一个按钮,按下该按钮时,操作方法会加载第二个视图,其中有一个标签,其中包含一些文本。 第一个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文件是默认的,没有添加任何代码。 当我在第一个视图中按下按钮时 - 它的视图消失,但第二个视图没有出现,只出现一个空白的白色视图,上面什么也没有。

美国能源部

I went through the material regarding this issue but still can't figure it out completely.
The main issue is that i am using this method but the view does not show...
The app is built out of two view - the main one (i added it as a subview to the main nib) -
Has of a text field where i write some text.
Has a button that when pressed, the action method loads a second view that has a label with some text in it.
the source code of the first 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

the .m of the first ViewController:
#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];
}

The second View is simply empty (has a label added to it in it's nib file):
#import

@interface TxtRunnerViewController : UIViewController {

}

@end

It's .m file is the default one, added no code to it.
When i press the button in the first view - it's view disappears but the second view does not appear instead, only a blank white view appears, nothing on it.

Doe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧瑾黎汐 2024-10-15 00:24:19

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

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

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

对于委托示例,在创建新项目时打开实用程序应用程序示例并了解其工作原理或在线搜索一些示例。

If you push the TxtRunnerViewController it will completely hide the first one, but it still exist in background. (without removeFromSuperview)

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

To remove the first view it's a bit more difficult. You would need to implement a delegate in RootViewController which will be fired after clicking the done Button of the first view. Then in your RootViewController some code is executed which displays the second view and removes the first view.

For a delegate sample open the Utility Application Sample when creating a new project and figure out how it works or search some examples online.

吝吻 2024-10-15 00:24:19

该行:
[self.viewremoveFromSuperview];

从其超级视图中删除视图

,然后调用

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

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

您需要将 trvc 添加到超级视图中。

The line:
[self.view removeFromSuperview];

Removes the view from its super

then the call

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

Adds trvc to it - but that view has been removed and not visible anymore.

You need to add trvc to the superview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文