UIscrollview委派

时间:2016-06-10 22:33:22

标签: ios swift uiscrollview delegates

我正在尝试在我的UIscrollview中的两个视图控制器之间传递数据。我正在尝试使用委派在Viewcontroller1Viewcontroller2之间发送数据。代表是Viewcontroller,而代理人是Viewcontroller1Viewcontroller2

在下面发布的代码中,当切换Viewcontroller1中的开关时,会使Viewcontroller2中的开关置于“关闭”状态。我继续得到

  

致命错误:在解包可选值时意外发现nil

我运行时出现

错误,但我不知道导致此问题的原因。有什么想法吗?

以下是包含Viewcontroller和子视图/子视图的Uiscrollview

 import UIKit

class ViewController: UIViewController, testing {


var vc1 = ViewController1(nibName: "ViewController1", bundle: nil)
var vc2 = ViewController2(nibName: "ViewController2", bundle: nil)



@IBOutlet weak var scrollView: UIScrollView!

func test1() {
    vc2.switch2.on = false
}

override func viewDidLoad() {
    super.viewDidLoad()



    self.addChildViewController(vc1)
    self.scrollView.addSubview(vc1.view)
    vc1.didMoveToParentViewController(self)



    var frame1 = vc2.view.frame
    frame1.origin.x = self.view.frame.size.width
    vc2.view.frame = frame1

    self.addChildViewController(vc2)
    self.scrollView.addSubview(vc2.view)
    vc2.didMoveToParentViewController(self)


   self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

这里是Viewcontoller1代码

    protocol testing{
func test1()
}
    class ViewController1: UIViewController {

var delegate:testing?

@IBOutlet weak var switch1: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
    let vc = ViewController()
    self.delegate = vc
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func switch1toggled(sender: AnyObject) {
    delegate?.test1()

}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

这里是Viewcontroller 2代码

import UIKit

class ViewController2: UIViewController {
@IBOutlet weak var switch2: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func switch2toggled(sender: AnyObject) {
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

对不起这篇长篇文章,我已经被困了一个星期,关于如何改变另一个开关的状态来切换另一个类中的开关,这是我找到的最有效的方式

1 个答案:

答案 0 :(得分:0)

试试这个:

ViewController1

class ViewController1: UIViewController {

  let defaults = NSUserDefaults.standardUserDefaults()

  let switch1Key = "view1Switch"

  override func viewWillAppear(animated: Bool) {
    view1Switch.on = defaults.boolForKey(switch1Key)
  }

  @IBOutlet weak var view1Switch: UISwitch!

  @IBAction func view1SwitchChanged(sender: UISwitch) {
    defaults.setBool(view1Switch.on, forKey: switch1Key)
  }

}

ViewController2

class ViewController2: UIViewController {

  let defaults = NSUserDefaults.standardUserDefaults()

  let switch1Key = "view1Switch"

  override func viewWillAppear(animated: Bool) {
    view2Switch.on = defaults.boolForKey(switch1Key)
  }

  @IBOutlet weak var view2Switch: UISwitch!

  @IBAction func view2SwitchChanged(sender: UISwitch) {
    defaults.setBool(view2Switch.on, forKey: switch1Key)
  }

}

此方法使用viewWillAppearNSUserdefaults同步两个UISwitch的状态。基本的思维模式是将开关的状态保存到NSUserdefaults,以便在实例化ViewController1或ViewController2时,view1Switch或view2Switch outlet的.on属性设置为保存的值。

警告:

  1. 实例化ViewController1(在第一个应用程序运行中)时,开关的第一个值将关闭,因为当没有保存的值时boolForKey返回false。在view1Switch.on = true

  2. 之后直接使用view1Switch.on = defaults.boolForKey(switch1Key)可能会导致攻击
  3. 此方法使开关具有相同的值。为了使它们具有不同的值,你可以使用!像ViewController2 view2Switch.on = !defaults.boolForKey(switch1Key)中的运算符。这样,开关1将始终与开关2相反。

  4. 我推荐这种方法优于委托,因为虽然委托是强大的,但这里似乎不需要它的力量。 如果您有任何问题,请询问! :d