有人可以告诉我:如何在 iphone sdk 的单个视图控制器中从一个视图导航到另一个视图?我有一个应用程序,我想在单个视图控制器中推送到下一个视图并弹出到上一个视图。如何实现此功能?提前致谢
问问题
4229 次
4 回答
2
您可以添加和删除您的第二视图。像这样导航
-(IBAction)navigate:(id)sender
{
[self.view addSubView:secondView];
}
这个是第一个视图
-(IBAction)popToFirstView:(id)sender
{
[secondView removeFromSuperView];
}
注意:- 如果要提供动画效果,可以使用动画来添加和删除视图。
于 2013-01-31T11:33:33.200 回答
1
如果您需要一个视图控制器,您可以尝试使用包含 2 个视图的 UIScrollView,以及一个从一个视图滚动到另一个视图的按钮......但如果我可以给出提示,最好使用 2 个视图控制器和一个导航视图控制器
于 2013-01-31T11:34:05.180 回答
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];
于 2013-01-31T12:11:08.037 回答
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
希望这将在未来对其他人有所帮助。
谢谢。
于 2013-01-31T11:58:40.693 回答