http://blog.csdn.net/woaifen3344/article/details/29883183
Models: UserModel.swift
Views: UserInfoCell.swift
Controllers: RootViewController.swift,DetailViewController.swift
AppDelegate.swift:
- importUIKit
- @UIApplicationMain
- classAppDelegate:UIResponder,UIApplicationDelegate{
- varwindow:UIWindow?
- funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{
- self.window=UIWindow(frame:UIScreen.mainScreen().bounds)
- //
- letrootController=RootViewController(style:UITableViewStyle.Plain)
- letrootNav=UINavigationController(rootViewController:rootController)
- self.window!.rootViewController=rootNav
- self.window!.backgroundColor=UIColor.whiteColor()
- self.window!.makeKeyAndVisible()
- returntrue
- }
- }
UserModel.swift
UserInfoCell.swift:
DetailViewController.swift:
copy
//thisdelegateneedsa@objc,because@optionalisonlyforobjective-c,notforswift
@objcprotocolDetailViewControllerDelegate:NSObjectProtocol{
@optionalfuncchangeItem(forUserModeluserModel:UserModel?)
classDetailViewController:UIViewController{
varuserModel:UserModel?
vardelegate:DetailViewControllerDelegate?
self.view.backgroundColor=UIColor.whiteColor()
self.title=userModel?.userName
letbutton=UIButton(frame:CGRectMake(10,300,40))
button.setTitle("change",forState:UIControlState.Normal)
button.backgroundColor=UIColor.redColor()
button.addTarget(self,action:"onChangeButtonClick:",forControlEvents:UIControlEvents.TouchUpInside)
self.view.addSubview(button)
funconChangeButtonClick(sender:UIButton!){
ifuserModel{
userModel!.userName="ChangeName"
//changeItemneedstoadda?totheend,before(),because
//thisfunctionisoptional
//delegate?表示可能没有代理,而changeItem?表示方法可能没有实现,这样写就算没有实现也没有问题
delegate?.changeItem?(forUserModel:userModel)
self.navigationController.popViewControllerAnimated(true)
}
效果图: