IOS 学习笔记

iOS学习笔记

  • 在IOS中所有的页面(view)都遵循MVC架构,即数据存放在model(具体的业务类)中,使用control(viewcontrol)控制数据,在view中显示

  • 使用当前页面控制器(viewcontrol)中实现prepareForSegue方法进行页面切换前的准备。其中可以根据跳转页面的SegueIdentifier来分辨跳转目标执行具体逻辑。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let identifier = segue.identifier {
            switch identifier{
            case History.SegueIdentifier:
                if let tvc = segue.destinationViewController as? TextViewController
                {
                    if let ppc = tvc.popoverPresentationController{
                    ppc.delegate = self
                    }
                    tvc.text = "\(diagnostircHistory)"
                }
            default: break
            }
        }
    }
  • 对于一些功能组件,为了实现其方法,首先需要为其设置一个代理(类)。该类需要继承该组件指定的特点协议,通过实现协议中的方法实现组件方法。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class  DiagnosedHappinewwViewController: HappinessViewController,UIPopoverPresentationControllerDelegate
{

    override var happiness: Int{
        didSet {
            diagnostircHistory += [happiness]
        }
    }

    //
    private let defaults = NSUserDefaults.standardUserDefaults()
    var diagnostircHistory:[Int]{
        get {return defaults.objectForKey(History.DefaultsKey) as? [Int] ?? [] }
        set {defaults.setObject(newValue, forKey: History.DefaultsKey) }
    }
    private struct History {

        static let SegueIdentifier = "Show Diagnostic History"
        static let DefaultsKey = "DiagnosedHappinessViewController.History"
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let identifier = segue.identifier {
            switch identifier{
            case History.SegueIdentifier:
                if let tvc = segue.destinationViewController as? TextViewController
                {
                    if let ppc = tvc.popoverPresentationController{
                    ppc.delegate = self
                    }
                    tvc.text = "\(diagnostircHistory)"
                }
            default: break
            }
        }
    }

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!, traitCollection: UITraitCollection!) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
}