iOS/Swift - 疯狂关注
iOS/Swift - Crazy Segue
我想扫描 ISBN 代码(图书标识符)以填充之前的视图。所以我执行一个 segue 回到我来自的视图。
疯狂的事情发生了。我看到了我的视图,当返回登录视图(初始视图)时,应用程序自动登录(因为已经注册)并在 tableView 停止,您可以在其中添加新书的添加按钮。
我不明白为什么在我的书 VC 之后我的应用程序跳转到初始 VC ?
ScanBookViewController.swift :
class ScanBookViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var book = Book()
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
guard captureSession.canAddInput(videoInput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addInput(videoInput)
let metadataOutput = AVCaptureMetadataOutput()
guard captureSession.canAddOutput(metadataOutput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code]
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func alertPopUp(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.running == false) {
captureSession.startRunning()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.running == true) {
captureSession.stopRunning()
}
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
foundCode(readableObject.stringValue)
}
dismissViewControllerAnimated(true, completion: nil)
}
func foundCode(code: String) {
book.setISBN(code)
book.setTitle("Super Titre")
book.setAuthor("Gil Felot")
book.setCover(UIImage(named: "iu.png")!)
performSegueWithIdentifier("infoFinded", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "infoFinded" {
let bookVC: BookViewController = segue.destinationViewController as! BookViewController
bookVC.book = book
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
}
BookViewController.swift :
class BookViewController: UIViewController {
var book:Book!
@IBOutlet weak var bookImage: UIImageView!
@IBOutlet weak var bookTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if book == nil {
print("test segue")
performSegueWithIdentifier("scanCode", sender: nil)
} else {
bookImage.image = book.getCover()
}
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
任何想法,因为我什至不知道去哪里看!
当您设置 segue 时,您 select 显示(又名推送)还是 select 模态?处理传递数据的标准方法是通过委托。创建一个协议并在 ScanBookViewController 中声明一个委托。然后将 BookViewController 设置为委托。扫描图书后调用委托方法,然后弹出或关闭 scanBookViewController。
protocol ScanBookDelegate{
func didScanBook(scannedBook: Book)
}
class ScanBookViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var book = Book()
var delegate: ScanBookDelegate? // create a new delegate instance
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
guard captureSession.canAddInput(videoInput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addInput(videoInput)
let metadataOutput = AVCaptureMetadataOutput()
guard captureSession.canAddOutput(metadataOutput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code]
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func alertPopUp(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.running == false) {
captureSession.startRunning()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.running == true) {
captureSession.stopRunning()
}
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
foundCode(readableObject.stringValue)
}
dismissViewControllerAnimated(true, completion: nil)
}
func foundCode(code: String) {
book.setISBN(code)
book.setTitle("Super Titre")
book.setAuthor("Gil Felot")
book.setCover(UIImage(named: "iu.png")!)
self.delegate?.didScanBook(book) //Call the delegate method
self.navigationController?.popViewControllerAnimated(true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "infoFinded" {
let bookVC: BookViewController = segue.destinationViewController as! BookViewController
bookVC.book = book
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
}
class BookViewController: UIViewController,ScanBookDelegate {
var book:Book!
@IBOutlet weak var bookImage: UIImageView!
@IBOutlet weak var bookTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if book == nil {
print("test segue")
performSegueWithIdentifier("scanCode", sender: nil)
} else {
bookImage.image = book.getCover()
}
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { //set book view controller as delegate to scan book view controller
if segue.destinationViewController is ScanBookViewController{
let scanBookViewController:ScanBookViewController = segue.destinationViewController
scanBookViewController.delegate = self
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
fun didScanBook(scannedBook:Book) //respond as delegate
{
self.book = scannedBook
}
}
我想扫描 ISBN 代码(图书标识符)以填充之前的视图。所以我执行一个 segue 回到我来自的视图。
疯狂的事情发生了。我看到了我的视图,当返回登录视图(初始视图)时,应用程序自动登录(因为已经注册)并在 tableView 停止,您可以在其中添加新书的添加按钮。
我不明白为什么在我的书 VC 之后我的应用程序跳转到初始 VC ?
ScanBookViewController.swift :
class ScanBookViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var book = Book()
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
guard captureSession.canAddInput(videoInput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addInput(videoInput)
let metadataOutput = AVCaptureMetadataOutput()
guard captureSession.canAddOutput(metadataOutput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code]
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func alertPopUp(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.running == false) {
captureSession.startRunning()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.running == true) {
captureSession.stopRunning()
}
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
foundCode(readableObject.stringValue)
}
dismissViewControllerAnimated(true, completion: nil)
}
func foundCode(code: String) {
book.setISBN(code)
book.setTitle("Super Titre")
book.setAuthor("Gil Felot")
book.setCover(UIImage(named: "iu.png")!)
performSegueWithIdentifier("infoFinded", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "infoFinded" {
let bookVC: BookViewController = segue.destinationViewController as! BookViewController
bookVC.book = book
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
}
BookViewController.swift :
class BookViewController: UIViewController {
var book:Book!
@IBOutlet weak var bookImage: UIImageView!
@IBOutlet weak var bookTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if book == nil {
print("test segue")
performSegueWithIdentifier("scanCode", sender: nil)
} else {
bookImage.image = book.getCover()
}
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
任何想法,因为我什至不知道去哪里看!
当您设置 segue 时,您 select 显示(又名推送)还是 select 模态?处理传递数据的标准方法是通过委托。创建一个协议并在 ScanBookViewController 中声明一个委托。然后将 BookViewController 设置为委托。扫描图书后调用委托方法,然后弹出或关闭 scanBookViewController。
protocol ScanBookDelegate{
func didScanBook(scannedBook: Book)
}
class ScanBookViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var book = Book()
var delegate: ScanBookDelegate? // create a new delegate instance
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
captureSession = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
let videoInput: AVCaptureDeviceInput
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
guard captureSession.canAddInput(videoInput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addInput(videoInput)
let metadataOutput = AVCaptureMetadataOutput()
guard captureSession.canAddOutput(metadataOutput) else {
alertPopUp("Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.")
captureSession = nil
return
}
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code]
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
func alertPopUp(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if (captureSession?.running == false) {
captureSession.startRunning()
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if (captureSession?.running == true) {
captureSession.stopRunning()
}
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
foundCode(readableObject.stringValue)
}
dismissViewControllerAnimated(true, completion: nil)
}
func foundCode(code: String) {
book.setISBN(code)
book.setTitle("Super Titre")
book.setAuthor("Gil Felot")
book.setCover(UIImage(named: "iu.png")!)
self.delegate?.didScanBook(book) //Call the delegate method
self.navigationController?.popViewControllerAnimated(true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "infoFinded" {
let bookVC: BookViewController = segue.destinationViewController as! BookViewController
bookVC.book = book
}
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
}
class BookViewController: UIViewController,ScanBookDelegate {
var book:Book!
@IBOutlet weak var bookImage: UIImageView!
@IBOutlet weak var bookTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if book == nil {
print("test segue")
performSegueWithIdentifier("scanCode", sender: nil)
} else {
bookImage.image = book.getCover()
}
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { //set book view controller as delegate to scan book view controller
if segue.destinationViewController is ScanBookViewController{
let scanBookViewController:ScanBookViewController = segue.destinationViewController
scanBookViewController.delegate = self
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
fun didScanBook(scannedBook:Book) //respond as delegate
{
self.book = scannedBook
}
}