Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. // MARK: - The `User` struct
  2.  
  3. struct User: Equatable {
  4.  
  5. /// The user's name.
  6. var name: String
  7.  
  8. /// The user's username.
  9. var username: String
  10.  
  11. init(name: String, username: String) {
  12. self.name = name
  13. self.username = username
  14. }
  15. }
  16.  
  17. func ==(lhs: User, rhs: User) -> Bool {
  18. return lhs.name == rhs.name &&
  19. lhs.username == rhs.username
  20. }
  21.  
  22. // MARK: - A sample view controller for the user
  23.  
  24. class UserViewController: UIViewController {
  25.  
  26. @IBOutlet weak var nameLabel: UILabel!
  27. @IBOutlet weak var usernameLabel: UILabel!
  28.  
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. }
  32. }
  33.  
  34. // MARK: - A sample tab bar controller
  35.  
  36. class TabBarController: UITabBarController {
  37.  
  38. // Create a list of users
  39. let users: [User] = [User(name: "Joe", username: "@joe34"),
  40. User(name: "Bob", username: "@bob123"),
  41. User(name: "Gus", username: "@__gus__")]
  42.  
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45.  
  46. var views: [UserViewController] = []
  47.  
  48. for user in self.users { // Iterate over the users
  49. let vc = self.storyboard?.instantiateViewController(withIdentifier: "UserViewController") as! UserViewController // Create a new view controller from the storyboard
  50. vc.loadViewIfNeeded() // This waits for the view controller to initialize
  51. vc.tabBarItem = UITabBarItem(title: user.name, image: nil, tag: self.users.index(of: user)!) // the `image` is nil, but you can fill it in later if need be.
  52. vc.nameLabel.text = user.name
  53. vc.usernameLabel.text = user.username
  54. views.append(vc) // Add this view controller to the list
  55. }
  56.  
  57. self.viewControllers = views // Assigns the list of view controllers to the tab bar, where they will be displayed
  58. }
  59. }
Add Comment
Please, Sign In to add comment