Frage
Wie navigiere ich von einem Ansichts-Controller zu einem anderen, indem Sie einfach das Touch-Up innerhalb eines Ereignisses einer Schaltfläche verwenden?
Mehr Info
Was ich in einem Beispielprojekt in Schritten versucht habe, war:
Erstellen Sie die Beispielanwendung für eine einzelne Ansicht.
Fügen Sie eine neue Datei hinzu -> Objective-C-Klasse mit XIB für die Benutzeroberfläche (ViewController2).
Fügen Sie eine Schaltfläche in ViewController.xib hinzu, und klicken Sie auf ViewController.h, um das Touch-In-Ereignis zu erstellen.
Gehen Sie zu der neu erstellten IBAction in ViewController.m und ändern Sie sie in diese ...
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
[[self navigationController] pushViewController:vc2 animated:YES];
}
Der Code läuft fehlerfrei und ich habe die Funktionalität der Schaltfläche mit NSLog getestet. Es navigiert mich jedoch immer noch nicht zum zweiten View-Controller. Jede Hilfe wäre dankbar.
Swift3
**Push**
mag es
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as NewsDetailsViewController
vc.newsObj = newsObj
navigationController?.pushViewController(vc,
animated: true)
oder sicherer
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController {
viewController.newsObj = newsObj
if let navigator = navigationController {
navigator.pushViewController(viewController, animated: true)
}
}
vorhanden
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewsDetailsVCID") as! NewsDetailsViewController
vc.newsObj = newsObj
present(vc!, animated: true, completion: nil)
oder sicherer
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsDetailsVCID") as? NewsDetailsViewController
{
vc.newsObj = newsObj
present(vc, animated: true, completion: nil)
}
//Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
//ViewController.m
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}
Swift
//Appdelegate.Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let navigat = UINavigationController()
let vcw = ViewController(nibName: "ViewController", bundle: nil)
// Push the vcw to the navigat
navigat.pushViewController(vcw, animated: false)
// Set the window’s root view controller
self.window!.rootViewController = navigat
// Present the window
self.window!.makeKeyAndVisible()
return true
}
//ViewController.Swift
@IBAction func GoToNext(sender : AnyObject)
{
let ViewController2 = ViewController2(nibName: "ViewController2", bundle: nil)
self.navigationController.pushViewController(ViewController2, animated: true)
}
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
MemberDetailsViewController* controller = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentiferInStoryBoard"];
[self.navigationController pushViewController:viewControllerName animated:YES];
Schnell 4:
let storyBoard = UIStoryboard(name: "storyBoardName", bundle:nil)
let memberDetailsViewController = storyBoard.instantiateViewController(withIdentifier: "viewControllerIdentiferInStoryBoard") as! MemberDetailsViewController
self.navigationController?.pushViewController(memberDetailsViewController, animated:true)
Verwenden Sie diesen Code in Ihrer Tastenaktion (Swift 3.0.1):
let vc = self.storyboard?.instantiateViewController(
withIdentifier: "YourSecondVCIdentifier") as! SecondVC
navigationController?.pushViewController(vc, animated: true)
Für Swift verwende den folgenden Code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.backgroundColor = UIColor.whiteColor()
// Create a nav/vc pair using the custom ViewController class
let nav = UINavigationController()
let vc = NextViewController(nibName: "NextViewController", bundle: nil)
// Push the vc onto the nav
nav.pushViewController(vc, animated: false)
// Set the window’s root view controller
self.window!.rootViewController = nav
// Present the window
self.window!.makeKeyAndVisible()
return true
}
ViewController:
@IBAction func Next(sender : AnyObject)
{
let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
self.navigationController.pushViewController(nextViewController, animated: true)
}
Wenn Sie diesen Code für das Navigieren zum nächsten Viewcontroller verwenden, wenn Sie ein Storyboard verwenden, folgen Sie diesem Code:
UIStoryboard *board;
if (!self.storyboard)
{
board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
}
else
{
board = self.storyboard;
}
ViewController *View = [board instantiateViewControllerWithIdentifier:@"yourstoryboardname"];
[self.navigationController pushViewController:View animated:YES];
Wenn Sie Swift verwenden:
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("controllerID")
self.navigationController!.pushViewController(controller, animated: true)
Das funktioniert perfekt:
PD: Denken Sie daran, den Ziel-VC zu importieren:
#import "DestinationVCName.h"
- (IBAction)NameOfTheAction:(id)sender
{
DestinationVCName *destinationvcname = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationVCName"];
[self presentViewController:destinationvcname animated:YES completion:nil];
}
Sagen wir Wenn Sie von ViewController A -> B dann gehen möchten
Stellen Sie sicher, dass Ihr ViewControllerA in den Navigationscontroller eingebettet ist
Wenn Sie auf ViewControllerA klicken, sollte der Code so aussehen.
@IBAction func goToViewController (_ Absender: Beliebig) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
Schau StoryboardID = ViewControllerB
UIViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboardId"];
[self.navigationController pushViewController:vc animated:YES];
AppDelegate für ViewController:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let loginPageView = mainStoryboard.instantiateViewControllerWithIdentifier("leadBidderPagerID") as! LeadBidderPage
var rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.pushViewController(loginPageView, animated: true)
Zwischen ViewControllern:
let loginPageView = self.storyboard?.instantiateViewControllerWithIdentifier("scoutPageID") as! ScoutPage
self.navigationController?.pushViewController(loginPageView, animated: true)
- (void) loginButton:(FBSDKLoginButton *)loginButton
didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
error:(NSError *)error{
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"nav"];
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"LoggedInVC"];
[nav pushViewController:vc animated:YES];
[self presentViewController:nav animated:YES completion:nil];
}
"nav" ist die Storyboard-ID für meinen Navigationscontroller "vc" ist die Storyboard-ID für meinen ersten mit meinem Navigationscontroller verbundenen View-Controller
-hoffe das hilft
UINavigationController wird nicht automatisch in UIViewController dargestellt.
Das sollten Sie im Interface Builder sehen. Der Besitzer der Dateien hat eine Ansicht zum Navigationscontroller und vom Navigationscontroller zur aktuellen Ansicht.
Swift 4 & 5
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourController") as! AlgoYoViewController
navigationController?.pushViewController(vc,
animated: true)