Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Will Macleod
Courses Plus Student 18,780 PointsHow to segue from a UILabel to an in app browser and show a specific link
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var funFactLabel: UILabel!
@IBOutlet weak var funFactButton: UIButton!
let factBook = FactBook()
let colorWheel = ColorWheel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
funFactLabel.text = factBook.randomFact()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showFunFact() {
var randomColor = colorWheel.randomColor()
view.backgroundColor = randomColor
funFactButton.tintColor = randomColor
funFactLabel.text = factBook.randomFact()
}
}
As you can see I initialise "factBook" and "colorWheel" which are structs that consists of an array of strings for the "factBook" and UIcolors for the "colorWheel". There is a function called "randomFact()" in the "factBook" struct that returns a random string from the array. Similarly there is a function called "randomColor()" in the "colorWheel" struct that returns a random UIcolor from the array.
You can see I have a "showFunFact()" function and a "funFactLabel" of type UILabel. Inside the "showFunFact()" function the "funFactLabel" is set to the return value of the "randomFact()" function. Same goes for the colorwheel and the text of the button. This is cool.
Here is what the "factBook" struct looks like:
/
// FactBook.swift
// FunFacts
//
// Created by Alex Macleod on 5/10/14.
// Copyright (c) 2014 Alex Macleod. All rights reserved.
//
import Foundation
struct FactBook {
let factsArray = [
"\"The secret of being a bore... is to tell everything.\" — Voltaire",
"\"The danger of success is that it makes us forget the world's dreadful injustice.\" — Jules Renard",
"\"Blessed is the man who expects nothing, for he shall never be disappointed.\" — Alexander Pope",
"\"As children, we played hide and seek with one another, as adults with ourselves.\" — Yahia Lababidi",
"“When you run from discomfort all the time, you are restricted to a small zone of comfort” — Leo Babauta",
"“To know you have enough is to be rich.” — Lao Tzu",
"“Success is achieved when you combine mind, body, and technique.” — Sensei Yoshizo Machida",
"“He who knows others is wise; He who know himself is enlightened.” — Lao Tzu",
"“Do not spoil what you have by desiring what you have not.” — Epicurus",
"\"If there is something that must be done, it is better not to rely on others.\" — Hagakure",
"\"It's not good to settle into a set of opinions; it's a mistake to put in effort and obtain some understanding and stop at that.\" — Hagakure",
"“People let the same problem make them miserable for years when they could just say 'so what'.” — Andy Warhol",
"“Man needs difficulties; they are necessary for health.” — Carl Jung",
"“Men of profound sadness betray themselves when they are happy.” — F. Nietzsche",
"“He who controls others may be powerful, but he who has mastered himself is mightier still.” — Lao Tzu",
"“Fiction is a branch of neurology: the scenarios of nerve and blood vessels are the written mythologies of memory and desire.” — J.G. Ballard",
"“Unhappy parents teach you a lesson that lasts a lifetime.” — J.G. Ballard",
"\"The less men think, the more they talk.\" — Montesquieu",
"“I had no shoes and complained, until I met a man who had no feet.” — Indian Proverb",
"“Fools talk, cowards are silent, wise men listen.” — Carlos Ruiz Zafón",
"“Zen is not some kind of excitement, but concentration on our usual everyday routine.” — Shunryu Suzuki",
"\"The man who does not read good books has no advantage over the man who can't read them.\" — Mark Twain",
"“He who laughs at himself never runs out of things to laugh at.” — Epictetus",
"“You should sit in meditation for twenty minutes a day, unless you are too busy. Then you should sit for an hour.” — Zen Proverb",
"“Train yourself to let go of everything you fear to lose.” — Yoda",
"“Muddy water is best cleared by leaving it alone.” — Alan Watts",
"“Maybe you have to know the darkness before you can appreciate the light.” — Madeleine L’Engle",
"“We can’t selectively numb emotion. Numb the dark and you numb the light.” — Brené Brown",
"“It is useless to hold a person to anything he says while he is in love, drunk, or running for office.” — Shirley Maclaine",
"“The awareness of emptiness brings forth a heart of compassion.” — Gary Snyder",
"\"Beware those who seek constant crowds for they are nothing alone.\" — Charles Bukowski",
"“Be brave. Take risks. Nothing can substitute experience.” — Paulo Coelho",
"“When you get there, there isn’t any there there.” — Zen Proverb",
"\"If you chase two rabbits, you will not catch either one.\" — Russian Proverb",
"“True friendship resists time, distance and silence.” — Isabel Allende",
"\"Inspiration exists, but it must find you working.\" — Pablo Picasso",
"“Expect more get less and feel sadness, or: expect less get more and feel happiness.” — Vijay Dhameliya",
"“Read books when you are free, read minds when you aren't.” — Rabindranath Tagore",
"“Definitions belong to the definers, not the defined.” — Toni Morrison",
"\"You meet your destiny on the road you take to avoid it.\" — Carl Jung",
"“We learn from history that we do not learn from history” — Hegel",
"“You should sit in meditation for twenty minutes a day, unless you are too busy. Then you should sit for an hour.” — Zen Proverb",
"“From my rotting body flowers shall grow and I am in them and that is eternity.” — Edvard Munch",
"“Great things are not accomplished by those who yield to trends and fads and popular opinion.” — Jack Kerouac",
"“Good people do not need laws to tell them to act responsibly.” — Plato",
"“If there is to be any peace it will come through being, not having.” — Henry Miller",
"\"Some people are born on third base and go through life thinking they hit a triple.\" — Barry Switzer",
"“You can tell more about a person by what he says about others than you can by what others say about him.” — Audrey Hepburn",
"“If you really want to see why you do things, then don’t do them and see what happens.” — Michael A. Singer",
"“When you’re afraid of someone’s judgment, you can’t connect with them. You’re too preoccupied with impressing them.” — Amanda Palmer",
"“Having no destination, I am never lost.” — Ikkyu",
"“Beliefs are neat. Cherish them, but don’t share them like they’re the truth.” — Bill Hicks",
"\"A day without laughter is a day wasted.\" — Charlie Chaplin",
"\"Now and then it's good to pause in our pursuit of happiness and just be happy.\" — Guillaume Apollinaire",
"\"Out of your vulnerabilities will come your strength.\" — Sigmund Freud",
"“When another person makes you suffer, it is because he suffers deeply within himself, and his suffering is spilling over.” — Thich Nhat Hanh",
"“Man suffers only because he takes seriously what’s made for fun.” — Alan Wilson Watts",
"“What would your current frustrations look like from the vantage point of the final days of your life?” — Taro Gold",
"“It does not matter how slowly you go as long as you do not stop.” — Confucius",
"“The only way to make sense out of change is to plunge into it, move with it, and join the dance.” — Alan Watts",
"“Creativity is the greatest rebellion.” — Osho"]
func randomFact() -> String? {
var arrayLength = UInt32(factsArray.count)
var randomNumber = Int(arc4random_uniform(arrayLength))
var finalRandomNumber = Int(randomNumber)
return factsArray[finalRandomNumber]
}
Here is what the "colorWheel" struct looks like:
import Foundation
import UIKit
struct ColorWheel {
let colorsArray = [
UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color
UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color
UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color
UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color
UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color
UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color
UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color
]
func randomColor() -> UIColor {
var arrayLength = UInt32(colorsArray.count)
var randomNumber = Int(arc4random_uniform(arrayLength))
var finalRandomNumber = Int(randomNumber)
return colorsArray[finalRandomNumber]
}
}
I want each quote in the "funFactArray" to have a corresponding hyperlink (url whatever) to a wikipedia page that I will hardcode. I'm thinking of using a dictionary for this but have no idea how to implement.
My question is how do I make it so that when the user taps the "funFactLabel" it will segue to the webpage of the wikipedia link which corresponds to that particular quote in the "funFactArray" that is currently displayed in the UILabel.