我已经以编程方式设置了此按钮,并且我一直试图弄清楚当它被按下以切换到另一个ViewController
时。我一直试图查找我需要的代码类型。我似乎找不到任何东西。
import UIKit
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "Camera.png") as UIImage!
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(self.view.frame.width / 2 - 30, self.view.frame.size.height - 70, 70, 70)
button.setImage(image, forState: .Normal)
self.navigationController?.view.addSubview(button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这就是我设置的方式,有人可以指导我使用我需要使用的正确类型的代码或给我一个例子。谢谢
答案 0 :(得分:2)
首先以这种方式为您的按钮添加操作:
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
之后添加辅助方法:
func buttonTapAction(sender:UIButton!){
//this code will navigate to next view when you press button.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(vc, animated: true)
}
完整代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "s.png") as UIImage!
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(self.view.frame.width / 2 - 30, self.view.frame.size.height - 70, 70, 70)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonTapAction(sender:UIButton!){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
查看THIS示例以获取更多信息。
答案 1 :(得分:0)
只需一行
self.navigationController!.pushViewController(self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! UIViewController, animated: true)
答案 2 :(得分:0)
从ViewController到TargetViewController创建一个手动segue。
现在,在按钮上添加一个动作。
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
在行动中这样做。
func buttonAction(sender:AnyObject)
{
self.performSegueWithIdentifier("manualSegue", sender: nil)
}
希望这会对你有所帮助。
答案 3 :(得分:0)
首先为此按钮创建一个动作..
- (IBAction)btnInvite:(id)sender {
}
然后导入viewcontroller,你想继续前进...
#import "YourViewController.h"
最后将代码放入您的按钮操作方法中,无论我们在上面创建了什么
- (IBAction)btnInvite:(id)sender {
YourViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"YourViewController Identifier Name"];
[self.navigationController pushViewController:viewController animated:YES];}
希望它适合你
答案 4 :(得分:0)
let VC = self.storyboard?.instantiateViewController(withIdentifier:"yourViewController") as! yourViewController
self.navigationController?.pushViewController(VC, animated:true)
仅使用您的Viewcontroller名称更改您的ViewController