Treehouse Intermediate Course - Part 19 (Build Interactive Story App Project 3)
Part 19! A good effort I'd say! I'm pleased to have stuck with Treehouse and to have no immediate need for moving on from it. Although it's several years out of date, compared to how the Swift language has evolved, it still seems relevant and more accessible than I thought. Anyway, not so good is my failure to get an entry in nearly a week. So today I'm getting in some, and will do so again tomorrow and/or Sunday. Back to the story app!
Start Time - 12:17
Page Controllers
A whole new section here!
Right - something new! How to programme views...
So a new Cocoa Class created - a subclass of view controller. Cool.
Awesome! The VC connected from the segue before is now linked to the Page Controller subclass; it works because changed the background colour to blue, which came up when the start button was clicked. Nice.
So getting more technical. We can make the page value an optional, but we don't want there to be the absence of a value. We don't want to use a dummy value either because that's not right...not too sure about the complexities of this at the moment.
Going with an optional needs unwrapping....
Right, so just to make sense of this, we need a stored property to initialise. So this is the code we have -
These inits apparently won't be used, it's just to get the initialisation underway. Clear as mud!
Prepare For Segue
Another confusing video! This was the code to get the segue to work...
Again all very long winded!
So I've got all the constraints programmed in, as well as having it not just on one line. It's becoming clearer how redundant this way of design is. I'm sure that in the latest Xcode/Swift/ use of UI that you would never need to learn or know this in depth. So moving on more!
Attributed Strings
So the NS Attributed String object manages the string. You can modify the string - font, line spacing etc. through the programming.
Out of date as well....but luckily someone has put in the updated Swift words.
This is vaguely interesting...but ultimately pointless as you can change all of these settings through the main storyboard surely...!
Making A Choice
So this was about setting the text so that each button has the relevant info. There was a difference - needed the ? after page as being an optional - must be a later Swift thing.
Continuing Our Story
So this goes against the DRY principle....
Someone in the comments has even given an alternative...but that still contained some repeating to an extent.
Finish Time - 18:09 (approx 1 hour 50 minutes)
So quite a long session in the end. I am exhausted with the programmatic code - it's so fiddly! But the advantage I guess is that you have everything controlled and can track every aspect more easily than when it's clicked and dragged around the screen. I definitely prefer the latter! But I've gone through this and can see the purpose, even though it's highly unlikely I am ever going to need to use this.
Start Time - 12:17
Page Controllers
A whole new section here!
Right - something new! How to programme views...
So a new Cocoa Class created - a subclass of view controller. Cool.
Awesome! The VC connected from the segue before is now linked to the Page Controller subclass; it works because changed the background colour to blue, which came up when the start button was clicked. Nice.
So getting more technical. We can make the page value an optional, but we don't want there to be the absence of a value. We don't want to use a dummy value either because that's not right...not too sure about the complexities of this at the moment.
Going with an optional needs unwrapping....
Right, so just to make sense of this, we need a stored property to initialise. So this is the code we have -
var page: Page?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(page: Page) {
self.page = page
super.init(nibName: nil, bundle: nil)
}
These inits apparently won't be used, it's just to get the initialisation underway. Clear as mud!
Prepare For Segue
Another confusing video! This was the code to get the segue to work...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "startAdventure" {
guard let pageController = segue.destination as? PageController else {
return }
pageController.page = Adventure.story
}
}
Annoyingly, this course has suddenly got much harder and is without clear explanation. I'm going to continue it but it is difficult to follow.
Creating Views Programmatically
So one of the advantages of doing it this way, even if it seems so verbose, is that you'll have a better understanding of the hierarchy - of the tree view explained before.
More code but all still unclear. I'm going to stick with this part and then review later!
Paused at 13:06
Continued at 16:58
Back! Going to do another half hour max as not using the screen! So far this has been a very, very confusing entry. Anyway, just want to get through this programmatic view part. Then review it to try to make some sort of sense of it!
Displaying The Story
So we have all of these anchor views put in -
Continued at 16:58
Back! Going to do another half hour max as not using the screen! So far this has been a very, very confusing entry. Anyway, just want to get through this programmatic view part. Then review it to try to make some sort of sense of it!
Displaying The Story
So we have all of these anchor views put in -
NSLayoutConstraint.activate([artworkView.topAnchor.constraint(equalTo: view.topAnchor), artworkView.bottomAnchor.constraint(equalTo: view.bottomAnchor), artworkView.rightAnchor.constraint(equalTo: view.rightAnchor),artworkView.leftAnchor.constraint(equalTo:view.leftAnchor),
])
}
Again all very long winded!
So I've got all the constraints programmed in, as well as having it not just on one line. It's becoming clearer how redundant this way of design is. I'm sure that in the latest Xcode/Swift/ use of UI that you would never need to learn or know this in depth. So moving on more!
Attributed Strings
So the NS Attributed String object manages the string. You can modify the string - font, line spacing etc. through the programming.
Out of date as well....but luckily someone has put in the updated Swift words.
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
This is vaguely interesting...but ultimately pointless as you can change all of these settings through the main storyboard surely...!
Making A Choice
So this was about setting the text so that each button has the relevant info. There was a difference - needed the ? after page as being an optional - must be a later Swift thing.
if let firstChoice = page?.firstChoice {
firstChoiceButton.setTitle(firstChoice.title, for: .normal)
} else {
firstChoiceButton.setTitle("Play again!", for: .normal)
}
if let secondChoice = page?.secondChoice {
secondChoiceButton.setTitle(secondChoice.title, for: .normal)
} else {
secondChoiceButton.setTitle("Play again!", for: .normal)
}
Continuing Our Story
So this goes against the DRY principle....
func loadFirstChoice() {
if let page = page, let firstChoice = page.firstChoice {
let nextPage = firstChoice.page
let pageController = PageController.init(page: nextPage)
navigationController?.pushViewController(pageController, animated: true)
}
}
func loadSecondChoice() {
if let page = page, let secondChoice = page.secondChoice {
let nextPage = secondChoice.page
let pageController = PageController.init(page: nextPage)
navigationController?.pushViewController(pageController, animated: true)
}
}
Someone in the comments has even given an alternative...but that still contained some repeating to an extent.
Finish Time - 18:09 (approx 1 hour 50 minutes)
So quite a long session in the end. I am exhausted with the programmatic code - it's so fiddly! But the advantage I guess is that you have everything controlled and can track every aspect more easily than when it's clicked and dragged around the screen. I definitely prefer the latter! But I've gone through this and can see the purpose, even though it's highly unlikely I am ever going to need to use this.
Comments
Post a Comment