如何在iphone sdk中的单个viewcontroller中从一个视图导航到另一个视图?

时间:2013-01-31 11:25:35

标签: iphone ios uinavigationcontroller

是的,有人让我知道: 如何在iphone sdk中的单个viewcontroller中从一个视图导航到另一个视图?我有一个应用程序,我想在一个View控制器中推送到下一个视图并弹出到上一个视图。如何实现这一功能? 提前致谢

4 个答案:

答案 0 :(得分:2)

您可以添加和删除第二个视图。就像导航一样

-(IBAction)navigate:(id)sender
{
  [self.view addSubView:secondView];
}

和这一到po来第一次观看

-(IBAction)popToFirstView:(id)sender
{
  [secondView removeFromSuperView];
}

注意: - 如果要提供动画效果,可以使用动画添加和删除视图。

答案 1 :(得分:1)

如果您需要单个视图控制器,您可以尝试使用包含2视图的UIScrollView,以及从视图滚动到另一个视图的按钮......但如果我可以给出提示,那么它会更好使用2视图控制器和导航视图控制器

答案 2 :(得分:1)

您需要使用UINavigation Controller来获取推送和弹出。

这样做

AppDelegate.h 文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//this will be your first screen
@property (strong, nonatomic) FavViewController *favViewController;

@end

在A * ppDelegate.m *文件

#import "AppDelegate.h"
#import "FavViewController.h"


@implementation AppDelegate

@synthesize window = _window;
@synthesize favViewController;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

        //initialize view controller
        favViewController = [[FavViewController alloc] initWithNibName:@"FavViewController" bundle:nil];

       //add navigation controller
        UINavigationController *favouriteView = [[UINavigationController alloc] initWithRootViewController:favViewController];  


       //add controller to witndow
        self.window.rootViewController = favouriteView;   
        [self.window makeKeyAndVisible];    

        return YES;
    }

@end

现在来到 UIViewController ,您要加载/推送到新控制器

加载/推送到新控制器使用此代码

//this will be next screen
DetailsViewController *detailsViewController = [[DetailsViewController alloc ]init];
[self.navigationController pushViewController:detailsViewController animated:YES];

返回或弹出控制器使用此

//now it will send back to parent screen
[self.navigationController popViewControllerAnimated:YES]; 

答案 3 :(得分:0)

我已经找到了解决这个问题的方法,下面是解决方案: -

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UIButton *backbtn;
    IBOutlet UIButton *nxtBtn;
    IBOutlet UILabel *label;
}
-(IBAction)nxt:(id)sender;
-(IBAction)ba:(id)sender;

@end

#import "ViewController.h"

@interface ViewController ()
@end

int count=0;

@implementation ViewController

- (void)viewDidLoad
{
    label.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"NavigtionNumber"];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
-(IBAction)nxt:(id)sender
{
    count++;
    label.text=[NSString stringWithFormat:@" navigation time %d",count];

    [[NSUserDefaults standardUserDefaults] setObject:label.text forKey:@"NavigtionNumber"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    ViewController *vc=[[ViewController alloc] init];

    [self.navigationController pushViewController:vc animated:YES];

}

-(IBAction)ba:(id)sender
{
    count--;
    label.text=[NSString stringWithFormat:@" navigation time %d",count];

    [[NSUserDefaults standardUserDefaults] setObject:label.text forKey:@"NavigtionNumber"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [self.navigationController popViewControllerAnimated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

希望这将有助于其他人。

感谢。