Skip to content

Commit 724bc3d

Browse files
committed
update for Swift 1.2, Xcode 6.3
1 parent 6cfad40 commit 724bc3d

File tree

16 files changed

+61
-43
lines changed

16 files changed

+61
-43
lines changed

bk2ch09p476popovers/ch22p751popovers/PopoverViewControllers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Popover1View1 : UITableViewController {
2626
}
2727

2828
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
29-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as UITableViewCell
29+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as! UITableViewCell
3030

3131
let section = indexPath.section
3232
let row = indexPath.row

bk2ch09p476popovers/ch22p751popovers/ViewController.swift

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ViewController : UIViewController {
3030
let b2 = UIBarButtonItem(barButtonSystemItem: .Done, target: self,
3131
action: "savePop1:")
3232
vc.navigationItem.leftBarButtonItem = b2
33-
let bb = UIButton.buttonWithType(.InfoDark) as UIButton
33+
let bb = UIButton.buttonWithType(.InfoDark) as! UIButton
3434
bb.addTarget(self, action:"doPresent:", forControlEvents:.TouchUpInside)
3535
bb.sizeToFit()
3636
vc.navigationItem.titleView = bb
@@ -44,7 +44,7 @@ class ViewController : UIViewController {
4444
// where's the configuration for the popover controller?
4545
// we can do it _after_ presentation
4646
if let pop = nav.popoverPresentationController { // self-contained; no need to retain or track!
47-
pop.barButtonItem = sender as UIBarButtonItem // who arrow points to
47+
pop.barButtonItem = sender as! UIBarButtonItem // who arrow points to
4848
pop.delegate = self
4949
// if you want to prevent toolbar buttons from being active
5050
// by setting passthroughViews to nil, you must use non-zero delayed performance
@@ -89,7 +89,7 @@ class ViewController : UIViewController {
8989
// show the popover (lower right button) and then rotate the interface
9090

9191
@IBAction func doButton(sender:AnyObject) {
92-
let v = sender as UIView
92+
let v = sender as! UIView
9393
let vc = UIViewController()
9494
vc.modalPresentationStyle = .Popover
9595
self.presentViewController(vc, animated: true, completion: nil)
@@ -122,10 +122,11 @@ class ViewController : UIViewController {
122122
let t = UITapGestureRecognizer(target:self, action:"tapped:")
123123
vc.view.addGestureRecognizer(t)
124124
if let pop = vc.popoverPresentationController {
125-
vc.modalInPopover = true
125+
// uncomment next line if you want there to be no way to dismiss!
126+
// vc.modalInPopover = true
126127
// we can dictate the background view
127128
pop.popoverBackgroundViewClass = MyPopoverBackgroundView.self
128-
pop.barButtonItem = sender as UIBarButtonItem
129+
pop.barButtonItem = sender as! UIBarButtonItem
129130
// we can force the popover further from the edge of the screen
130131
// silly example: just a little extra space at this popover's right
131132
// but it isn't working; this may be an iOS 8 bug
@@ -147,7 +148,7 @@ class ViewController : UIViewController {
147148
vc.view.frame = CGRectMake(0,0,300,300)
148149
vc.view.backgroundColor = UIColor.whiteColor()
149150
vc.preferredContentSize = vc.view.bounds.size
150-
let b = UIButton.buttonWithType(.System) as UIButton
151+
let b = UIButton.buttonWithType(.System) as! UIButton
151152
b.setTitle("Done", forState:.Normal)
152153
b.sizeToFit()
153154
b.center = CGPointMake(150,150)
@@ -161,17 +162,25 @@ class ViewController : UIViewController {
161162
presenter.presentViewController(vc, animated:true, completion:{
162163
_ in println("presented")
163164
})
164-
vc.modalInPopover = true // no effect
165-
//self.currentPop.passthroughViews = nil;
165+
//vc.modalInPopover = true // no effect
166166
delay(1) {
167+
// change in iOS 8.3!
168+
// I was trying to show that even though presented-in-popover v.c. _is_ modal in popover...
169+
// ... this has no effect: tapping outside dismisses _both_, including the popover
170+
// that was in iOS 8.1 (and 8.2?), but this is no longer true in 8.3
171+
// what happens now is that, as in iOS 7, presented-in-popover v.c. is modal once again
172+
// tapping outside the popover dismisses the presented-in-popover v.c.
173+
// but _not_ the popover itself
174+
// more about this in a later example
175+
println(presenter.popoverPresentationController!.passthroughViews)
167176
println(vc.modalInPopover)
168177
}
169178
}
170179

171180
func done (sender:AnyObject) {
172-
var r = sender as UIResponder
181+
var r = sender as! UIResponder
173182
do { r = r.nextResponder()! } while !(r is UIViewController)
174-
(r as UIViewController).dismissViewControllerAnimated(true, completion: {
183+
(r as! UIViewController).dismissViewControllerAnimated(true, completion: {
175184
println("dismissed")
176185
})
177186
}
@@ -213,6 +222,15 @@ extension ViewController : UIPopoverPresentationControllerDelegate {
213222
view.memory = self.button2
214223
}
215224
}
225+
226+
// uncomment to prevent tap outside present-in-popover from dismissing presented controller
227+
228+
func popoverPresentationControllerShouldDismissPopover(
229+
pop: UIPopoverPresentationController) -> Bool {
230+
let ok = pop.presentedViewController.presentedViewController == nil
231+
return ok
232+
}
233+
216234

217235
}
218236

bk2ch09p477popoversOnPhone/PopoverOnPhone/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class ViewController: UIViewController {
2121
wv.frame = vc.view.bounds
2222
wv.autoresizingMask = .FlexibleWidth | .FlexibleHeight
2323
let f = NSBundle.mainBundle().pathForResource("linkhelp", ofType: "html")
24-
let s = NSString(contentsOfFile: f!, encoding: NSUTF8StringEncoding, error: nil)
24+
let s = String(contentsOfFile: f!, encoding: NSUTF8StringEncoding, error: nil)
2525
wv.loadHTMLString(s, baseURL: nil)
2626

2727
if let pop = vc.popoverPresentationController {
28-
pop.sourceView = (sender as UIView)
29-
pop.sourceRect = (sender as UIView).bounds
28+
pop.sourceView = (sender as! UIView)
29+
pop.sourceRect = (sender as! UIView).bounds
3030
}
3131

3232
// that alone is completely sufficient, on iOS 8, for iPad and iPhone!

bk2ch09p481popoverInStoryboard/ch22p751popovers/Popover1View1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Popover1View1 : UITableViewController {
2424
}
2525

2626
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
27-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as UITableViewCell
27+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as! UITableViewCell
2828

2929
let section = indexPath.section
3030
let row = indexPath.row

bk2ch09p481popoverInStoryboard/ch22p751popovers/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ViewController : UIViewController, UIToolbarDelegate {
2626

2727
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
2828
println(segue)
29-
let dest = segue.destinationViewController as UIViewController
29+
let dest = segue.destinationViewController as! UIViewController
3030
if let pop = dest.popoverPresentationController {
3131
pop.delegate = self
3232
delay(0.1) {

bk2ch09p485PopoverPresentingViewController/DeleteMe/ViewController2.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class ViewController2: UIViewController, UIPopoverPresentationControllerDelegate
88

99
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
1010
println("prepare") // make sure we're called
11-
let dest = segue.destinationViewController as UIViewController
11+
let dest = segue.destinationViewController as! UIViewController
1212
dest.modalInPopover = true // does nothing
1313
self.modalInPopover = true // desperate, still does nothing
1414
if let pop = self.popoverPresentationController {
1515
println("del")
16-
pop.delegate = self
16+
pop.delegate = self // comment out this line to see what happens without the workaround
1717
}
1818
}
1919

bk2ch09p487searchInPopover/ch21p718sections/RootViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class RootViewController : UITableViewController, UISearchBarDelegate {
88
var searcher = UISearchController()
99

1010
override func viewDidLoad() {
11-
let s = NSString(contentsOfFile: NSBundle.mainBundle().pathForResource("states", ofType: "txt")!, encoding: NSUTF8StringEncoding, error: nil)!
12-
let states = s.componentsSeparatedByString("\n") as [String]
11+
let s = String(contentsOfFile: NSBundle.mainBundle().pathForResource("states", ofType: "txt")!, encoding: NSUTF8StringEncoding, error: nil)!
12+
let states = s.componentsSeparatedByString("\n")
1313
var previous = ""
1414
for aState in states {
1515
// get the first letter
@@ -70,7 +70,7 @@ class RootViewController : UITableViewController, UISearchBarDelegate {
7070
}
7171

7272
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
73-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
73+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
7474
let s = self.sectionData[indexPath.section][indexPath.row]
7575
cell.textLabel!.text = s
7676

@@ -94,7 +94,7 @@ class RootViewController : UITableViewController, UISearchBarDelegate {
9494
*/
9595
// this is more "interesting"
9696
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
97-
let h = tableView.dequeueReusableHeaderFooterViewWithIdentifier("Header") as UITableViewHeaderFooterView
97+
let h = tableView.dequeueReusableHeaderFooterViewWithIdentifier("Header") as! UITableViewHeaderFooterView
9898
if h.tintColor != UIColor.redColor() {
9999
// println("configuring a new header view") // only called about 7 times
100100
h.tintColor = UIColor.redColor() // invisible marker, tee-hee
@@ -123,7 +123,7 @@ class RootViewController : UITableViewController, UISearchBarDelegate {
123123
NSLayoutConstraint.constraintsWithVisualFormat("V:|[lab]|",
124124
options:nil, metrics:nil, views:["lab":lab]))
125125
}
126-
let lab = h.contentView.viewWithTag(1) as UILabel
126+
let lab = h.contentView.viewWithTag(1) as! UILabel
127127
lab.text = self.sectionNames[section]
128128
return h
129129

bk2ch09p487searchInPopover/ch21p718sections/SearchResultsController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class SearchResultsController : UITableViewController {
4747
}
4848

4949
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
50-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
50+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
5151
cell.textLabel!.text = self.filteredData[indexPath.row]
5252
return cell
5353
}

bk2ch09p488basicMasterDetail/MasterDetail/MasterViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MasterViewController: UITableViewController {
2020
}
2121

2222
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
23-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
23+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
2424
cell.textLabel!.text = model[indexPath.row]
2525
return cell
2626
}

bk2ch09p488universalSplitViewControllerStoryboardTemplateAnalysis/UniversalStoryboard/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2424

2525
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
2626
// Override point for customization after application launch.
27-
let svc = self.window!.rootViewController as UISplitViewController
27+
let svc = self.window!.rootViewController as! UISplitViewController
2828
// place button in detail controller's nav bar
29-
let nav = svc.viewControllers.last as UINavigationController
29+
let nav = svc.viewControllers.last as! UINavigationController
3030
// new in iOS 8, split v.c. vends the button with a method
3131
// note duplication!
3232
// the button will be added when the user taps a row in the master v.c....

bk2ch09p488universalSplitViewControllerStoryboardTemplateAnalysis/UniversalStoryboard/MasterViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MasterViewController: UITableViewController {
5252
if segue.identifier == "showDetail" {
5353
let ip = self.tableView.indexPathForSelectedRow()!
5454
let object = objects[ip.row] as NSDate
55-
let controller = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
55+
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
5656
println("prepare for segue")
5757
println("object: \(object)")
5858
controller.detailItem = object
@@ -77,7 +77,7 @@ class MasterViewController: UITableViewController {
7777
}
7878

7979
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
80-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
80+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
8181
let object = objects[indexPath.row] as NSDate
8282
cell.textLabel!.text = object.description
8383
return cell

bk2ch09p489universalSplitViewControllerStoryboard2/UniversalStoryboard/ContainerViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ContainerViewController : UIViewController {
4141
if !self.didInitialSetup {
4242
self.didInitialSetup = true
4343
self.view.backgroundColor = UIColor.greenColor()
44-
let svc = self.childViewControllers[0] as UISplitViewController
44+
let svc = self.childViewControllers[0] as! UISplitViewController
4545
svc.preferredDisplayMode = .AllVisible
4646
// if not collapsed, always side by side
4747
// if you insert the display mode button, it gives the option to hide the master column
@@ -65,7 +65,7 @@ class ContainerViewController : UIViewController {
6565
// don't override traits on launch (so, portrait, collapsed)
6666
// but do override just in case we rotate to landscape
6767
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
68-
let svc = self.childViewControllers[0] as UISplitViewController
68+
let svc = self.childViewControllers[0] as! UISplitViewController
6969
if which == 2 {
7070
if size.width > 320 {
7171
// landscape
@@ -85,7 +85,7 @@ extension ContainerViewController : UISplitViewControllerDelegate {
8585
// as in the template, we must take action to prevent the detail from being pushed and shown
8686
// in collapsed mode
8787

88-
func splitViewController(svc: UISplitViewController!,
88+
func splitViewController(svc: UISplitViewController,
8989
collapseSecondaryViewController vc2: UIViewController!,
9090
ontoPrimaryViewController vc1: UIViewController!) -> Bool {
9191
return true

bk2ch09p489universalSplitViewControllerStoryboard2/UniversalStoryboard/MasterViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MasterViewController: UITableViewController {
4848
if segue.identifier == "showDetail" {
4949
let ip = self.tableView.indexPathForSelectedRow()!
5050
let object = objects[ip.row] as NSDate
51-
let controller = (segue.destinationViewController as UINavigationController).topViewController as DetailViewController
51+
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
5252
println("prepare for segue")
5353
println(object)
5454
controller.detailItem = object
@@ -74,7 +74,7 @@ class MasterViewController: UITableViewController {
7474
}
7575

7676
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
77-
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
77+
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
7878
let object = objects[indexPath.row] as NSDate
7979
cell.textLabel!.text = object.description
8080
return cell

bk2ch09p490universalSplitViewControllerManual/SplitViewControllerManual/PrimaryViewController.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ class PrimaryViewController : UIViewController {
4444
// note this expression of the difference as to where the Secondary will be,
4545
// depending whether the svc is expanded or collapsed
4646
if !self.splitViewController!.collapsed {
47-
vc = self.splitViewController!.viewControllers[1] as UIViewController
47+
vc = self.splitViewController!.viewControllers[1] as! UIViewController
4848
} else {
49-
vc = self.childViewControllers[0] as UIViewController
49+
vc = self.childViewControllers[0] as! UIViewController
5050
}
51-
let seg = sender as UISegmentedControl
51+
let seg = sender as! UISegmentedControl
5252
switch seg.selectedSegmentIndex {
5353
case 0:
5454
vc.view.backgroundColor = UIColor.whiteColor()
@@ -76,7 +76,7 @@ class PrimaryViewController : UIViewController {
7676
NSLayoutConstraint.constraintsWithVisualFormat("H:|[v]|", options: nil, metrics: nil, views: ["v":vc2.view])
7777
)
7878
self.verticalConstraints =
79-
(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v]|", options: nil, metrics: nil, views: ["v":vc2.view]) as [NSLayoutConstraint])
79+
(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v]|", options: nil, metrics: nil, views: ["v":vc2.view]) as! [NSLayoutConstraint])
8080
self.view.addConstraints(self.verticalConstraints!)
8181
}
8282
}
@@ -124,15 +124,15 @@ extension PrimaryViewController {
124124
}
125125
}
126126
else {
127-
let vc2 = sender as UIViewController
127+
let vc2 = sender as! UIViewController
128128
var con = 0
129129
if !self.exposed {
130130
con = 270
131131
}
132132
self.exposed = !self.exposed
133133
self.view.removeConstraints(self.verticalConstraints!)
134134
self.verticalConstraints = (
135-
NSLayoutConstraint.constraintsWithVisualFormat("V:|-(minuscon)-[v]-(con)-|", options: nil, metrics: ["con":con, "minuscon":-con], views: ["v":vc2.view]) as [NSLayoutConstraint]
135+
NSLayoutConstraint.constraintsWithVisualFormat("V:|-(minuscon)-[v]-(con)-|", options: nil, metrics: ["con":con, "minuscon":-con], views: ["v":vc2.view]) as! [NSLayoutConstraint]
136136
)
137137
self.view.addConstraints(self.verticalConstraints!)
138138
UIView.animateWithDuration(0.25, animations: {

bk2ch09p490universalSplitViewControllerManual/SplitViewControllerManual/SecondaryViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SecondaryViewController : UIViewController {
88
override func viewDidLoad() {
99
super.viewDidLoad()
1010
self.view.backgroundColor = UIColor.redColor()
11-
let b = UIButton.buttonWithType(.System) as UIButton
11+
let b = UIButton.buttonWithType(.System) as! UIButton
1212
b.setTitle("Configure", forState: .Normal)
1313
b.addTarget(self, action: "callShowHide:", forControlEvents: .TouchUpInside)
1414
self.view.addSubview(b)

bk2ch09p490universalSplitViewControllerManual/SplitViewControllerManual/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class ViewController: UIViewController {
3333

3434
override func targetViewControllerForAction(action: Selector, sender: AnyObject?) -> UIViewController? {
3535
if action == "showHide:" {
36-
let svc = self.childViewControllers[0] as UISplitViewController
37-
let primary = svc.viewControllers[0] as UIViewController
36+
let svc = self.childViewControllers[0] as! UISplitViewController
37+
let primary = svc.viewControllers[0] as! UIViewController
3838
if primary.canPerformAction(action, withSender: sender) {
3939
return primary
4040
}

0 commit comments

Comments
 (0)