从一个视图到另一个视图

时间:2010-09-21 09:06:13

标签: iphone

我有一个问题...我正在开发基于导航的应用程序......当我在第二个屏幕中按下按钮时......显示一个新视图

button=[[UIButton alloc]initWithFrame:frame];
button.frame = CGRectMake(60,250,200,69);
[button setTitle:@"Crack Now" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.opaque;
[self.view addSubview:button];

-(void)buttonClicked{
    secondViewcontroller=[[SecondViewController alloc]initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:secondViewcontroller animated:YES];
    [self.navigationController presentModalViewController:secondViewcontroller animated:YES];
}

-(void)viewDidLoad{

    CGRect frame=CGRectMake(80,0,200,100);
    label=[[UILabel alloc] initWithFrame:frame];
    label.text = @"text";
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blueColor];
    label.textAlignment = UITextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:label];
    myImage=[[UIImageView alloc]initWithFrame:frame];
    myImage.frame=CGRectMake(90,70,150,170);
    [myImage setImage:[UIImage imageNamed:@"Untitled-3.jpg"]];
    myImage.opaque;
    [self.view addSubview:myImage];

    [super viewDidLoad];            
}

2 个答案:

答案 0 :(得分:1)

我真的不明白你在这里问的是什么,但我可以看到你的代码出了问题:

[self.navigationController pushViewController:secondViewcontroller animated:YES];
[self.navigationController presentModalViewController:secondViewcontroller animated:YES];

你应该只使用其中一个......

推送视图控制器意味着新视图将被推送到接收器(self.navigationcontroller)堆栈。换句话说,它将推入一个新视图,导航栏将显示一个后退按钮到你最后一个视图。

现在的模态视图控制器意味着它将向用户呈现由给定视图控制器管理的模态视图(self.navigationController)。模态视图没有后退按钮。你必须调用[self dismissModalViewController];再次删除它。

编辑: 您还应该在推送或呈现模态后释放secondViewController,以释放内存.. [secondViewController release];

答案 1 :(得分:0)

而我所看到的是,只要您点击该按钮,您的应用程序就会崩溃。因为你的方法

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
必须是 科隆不能在那里。因为你已经定义了没有任何发件人作为参数的方法

[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];

并且剩下的被告知如上

-(void)buttonClicked{
    secondViewcontroller=[[SecondViewController alloc]initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:secondViewcontroller animated:YES];
//    [self.navigationController presentModalViewController:secondViewcontroller animated:YES];
     [secondViewcontroller release];
}

快乐的编码......