Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - The `User` struct
- struct User: Equatable {
- /// The user's name.
- var name: String
- /// The user's username.
- var username: String
- init(name: String, username: String) {
- self.name = name
- self.username = username
- }
- }
- func ==(lhs: User, rhs: User) -> Bool {
- return lhs.name == rhs.name &&
- lhs.username == rhs.username
- }
- // MARK: - A sample view controller for the user
- class UserViewController: UIViewController {
- @IBOutlet weak var nameLabel: UILabel!
- @IBOutlet weak var usernameLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- }
- // MARK: - A sample tab bar controller
- class TabBarController: UITabBarController {
- // Create a list of users
- let users: [User] = [User(name: "Joe", username: "@joe34"),
- User(name: "Bob", username: "@bob123"),
- User(name: "Gus", username: "@__gus__")]
- override func viewDidLoad() {
- super.viewDidLoad()
- var views: [UserViewController] = []
- for user in self.users { // Iterate over the users
- let vc = self.storyboard?.instantiateViewController(withIdentifier: "UserViewController") as! UserViewController // Create a new view controller from the storyboard
- vc.loadViewIfNeeded() // This waits for the view controller to initialize
- 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.
- vc.nameLabel.text = user.name
- vc.usernameLabel.text = user.username
- views.append(vc) // Add this view controller to the list
- }
- self.viewControllers = views // Assigns the list of view controllers to the tab bar, where they will be displayed
- }
- }
Add Comment
Please, Sign In to add comment