在iphone中切换视图

时间:2011-05-17 06:53:39

标签: iphone objective-c cocoa-touch

我正在创建一个客户端服务器程序,我想从一个视图切换到另一个视图,但我在“clientserverprogram view.m”中收到错误.PLZ help

"clientserverprogram view.h" 

#import <UIKit/UIKit.h>

@class secondview;

@interface clientserverprogramViewController : UIViewController {

    IBOutlet UITextField *name;
    IBOutlet UITextView *filepath;
    IBOutlet UIButton *print;
    IBOutlet UIButton *settings;
    IBOutlet UIButton *cancel;
    IBOutlet UILabel *display;
    IBOutlet secondview *secondview;
}

-(IBAction) print;
-(IBAction) settings;
-(IBAction) cancel;

@property (nonatomic , retain) IBOutlet UITextField *name;
@property (nonatomic , retain) IBOutlet UITextView *filepath;

@property (nonatomic , retain) IBOutlet UILabel *display;
@end




"clientserverprogram view.m"


#import "clientserverprogramViewController.h"
#import "secondview.h"


@implementation clientserverprogramViewController

@synthesize  name ,filepath,display ;

-(IBAction) print {

    NSString *str = name.text;


    [display setText : str];

}

-(IBAction) settings {


    [self presentModalViewController: secondview animated: YES ];

"" error: expected expression before 'secondview'"" 


}


-(IBAction) cancel {


    exit(0);

}


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

@end


"secondview.h"


#import <UIKit/UIKit.h>


@interface secondview : UIViewController {

    IBOutlet UIView *view;

    IBOutlet UIButton *back;

}

-(IBAction) back;



@end



""secondview.m""


#import "secondview.h"


@implementation secondview




-(IBAction) back {

    [self.parentViewController dismissModalViewControllerAnimated: YES];
}


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


@end

3 个答案:

答案 0 :(得分:2)

[self presentModalViewController: secondview animated: YES ];

"" error: expected expression before 'secondview'""

这行代码负责呈现模态ViewController。在您的情况下,您只有一个视图。所以你要么为第二个视图创建一个控制器:

SecondViewController *secondController=[[SecondViewController  alloc] initWithNibName:@"secondView" bundle:[NSBundle mainBundle]];
 [self presentModalViewController:secondController animated: YES ];

或者您可以使用outlet从nib文件加载第二个视图,然后将其作为子视图添加到当前视图控制器视图中:

[self.view addSubView:secondview]; 

更新

正如我在您的代码中看到的那样,SecondView是您的控制器而不是视图,但您尝试呈现未初始化的控制器。我还注意到你有一个关于SecondView视图的插座,当你创建一个新的UIViewController子类时,你也可以检查创建.xib文件的选项。

希望有所帮助,

答案 1 :(得分:1)

如果您没有使用NIB文件,那么只需使用init mathod创建第二页的对象 并使用此

[self.view addsudView:secondView.view];

答案 2 :(得分:0)

我使用了你的代码,这里的问题是你的班级名称“secondview”和你正在制作的实例IBOutlet secondview * secondview是一样的。请为类名和您创建的实例使用不同的名称。

始终以类的大写字母开头,并以您创建的类实例的小写字母开头。因此,您的类名应该是SecondView,您应该编写IBOutlet SecondView * secondView。

或者您应该使用不同的名称。它非常令人困惑。