2013-01-31 37 views
0

可能有人讓我知道: 如何從一個視圖導航到另一個視圖在iphone中的單個viewcontroller sdk?我有一個應用程序,我想推到下一個視圖,並彈出到單個視圖控制器中的前一個視圖。如何實現這一功能? 在此先感謝如何在iphone sdk中的單個viewcontroller中從一個視圖導航到另一個視圖?

+0

我不知道,你可以使用導航控制器將視圖導航到單個視圖控制器,的確在self.myNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];您必須將視圖控制器傳遞給init,而不是視圖 – Lucabro

+0

爲此,您需要通過AppDelegate添加導航控制器。 – Krunal

回答

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 

希望這將幫助其他人的未來。

謝謝。

+0

好的,但是在此您不在單個視圖控制器中導航槽2視圖,您使用2個同一個視圖控制器一個在另一個內部聲明,並且這個按壓和彈出...反正酷的方式,但是pheraps可以有用改變一點問題 – Lucabro

+0

OK哥們..你也可以在上面的代碼導航期間更改視圖.. – Lalit

+0

我想你可以在上面的代碼中更改viewController,然後改變視圖,當然我的錯誤.... buddy :) – Lucabro

2

您可以添加和刪除您secondview.Like這個導航

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

,這一個PO到第一種觀點

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

注: - 您可以使用動畫的添加和刪​​除視圖,如果你想給動畫效果。

+0

我想在單個視圖控制器中使用導航控制器 – Lalit

1

如果您需要一個視圖控制器,您可以嘗試使用包含2視圖的UIScrollView以及從視圖滾動到另一個視圖的按鈕......但如果我可以給出提示,最好使用2視圖控制器和導航視圖控制器

+0

我想在單個視圖控制器中使用導航控制器,並導航視圖 – Lalit

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 *文件

​​

現在來看看的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]; 
相關問題