Swift-从View跳转页面+实用技巧

作者: 超人猿 | 来源:发表于2016-08-18 11:19 被阅读1483次

导语:

    好久没有写简书了,最近忙着写项目和自学Swift。希望在不久的将来可以把项目开源共享给大家,并加入OC和Swift两个版本。一直以来有一个小小的愿望就是写通俗易懂的项目和Demo供新手学习,让新手少走我们以前走的弯路。
好了,不多说,今天说说一些实用又简单的知识点吧

一、从View跳转页面

1.在OC中,我们从View跳逆到ViewController是这样的:

 ViewController *vc =[[ViewController alloc]init];
    UINavigationController *nc = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    [nc presentViewController:vc animated:YES completion:nil];

PS:简单的说就是先拿到根视图再跳转,因为你此刻在View上。

2.在Swift中,我们这样做

let path = NSBundle .mainBundle().pathForResource("惊天魔盗团2", ofType: "mp4")
        playerView = AVPlayer(URL:NSURL(fileURLWithPath: path!))
        playViewController.player = playerView

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(playViewController, animated: true, completion: nil)```
PS:跟OC道理是一样的,但Swift要注意其中的可选类型

二、注册Cell (自定义)

1.在OC中,这样注册:

[self.tableView registerNib:[UINib nibWithNibName:@"CRUserCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cellID"];

2.而Swift:

 tableView .registerNib(UINib (nibName: "VideoCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell")

三、去掉多余Cell的技巧

1.oc:

self.tableView.tableFooterView = [[UIView alloc]init];

2.Swift

tableView.tableFooterView = UIView()

总结:感受到用swift写代码的简洁性没?这就是swift的魅力。。。
后续更新

相关文章

网友评论

    本文标题:Swift-从View跳转页面+实用技巧

    本文链接:https://www.haomeiwen.com/subject/jtjvsttx.html