Creating UITabBarItem from an array












1















I'm writing an app that needs to create a number of tabs based on values from a database. It gets given a list of Pupils and Class numbers from a CSV file, and imports them into a database. I can run a SELECT DISTINCT on the database and I get a list of class numbers (eg -2, -1, R, 1, 2) and I can iterate through the list fine using



for class in classnumbers {

print (class[0])

}


and see a list of my class numbers, but I'm banging my head against a brick wall trying to generate the tabs at runtime. I've got the code:



    let firstVc = UIViewController()
firstVc.title = "First"
firstVc.tabBarItem = UITabBarItem.init(title: "Home", image: UIImage(named: "HomeTab"), tag: 0)

tabBarCont.viewControllers = [firstVc]


This will create a tab when it loads like I expect, so I know that works, and I can add secondVC etc to create more... What I'm struggling with is using my class number instead of firstVC - so I'd like to be able to generate them like this:



for class in classnumbers {

let class[0]Vc = UIViewController()
class[0]Vc.title = "class[0]"
class[0]Vc.tabBarItem = UITabBarItem.init(title: "class[0]", image: UIImage(named: "class[0]"), tag: 0)
tabBarCont.viewControllers = [class[0]Vc]

}


and so on for each class in the database. I've seen the question answered with "use an array", but I can't see how that would be any different to looping round my database results, and I've seen "you can't do that with swift" - so is it do-able or do I need to come up with a different way of looking at the problem? (I won't know the class numbers / names that a school use until after getting their data - if I have to hard code them, I'd have to recompile the app for every different school?)










share|improve this question























  • Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

    – Dom
    Nov 19 '18 at 22:48











  • I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

    – RobDag
    Nov 19 '18 at 22:54











  • Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

    – Myk
    Nov 20 '18 at 0:35











  • No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

    – RobDag
    Nov 20 '18 at 7:57











  • I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

    – Dom
    Nov 20 '18 at 15:48
















1















I'm writing an app that needs to create a number of tabs based on values from a database. It gets given a list of Pupils and Class numbers from a CSV file, and imports them into a database. I can run a SELECT DISTINCT on the database and I get a list of class numbers (eg -2, -1, R, 1, 2) and I can iterate through the list fine using



for class in classnumbers {

print (class[0])

}


and see a list of my class numbers, but I'm banging my head against a brick wall trying to generate the tabs at runtime. I've got the code:



    let firstVc = UIViewController()
firstVc.title = "First"
firstVc.tabBarItem = UITabBarItem.init(title: "Home", image: UIImage(named: "HomeTab"), tag: 0)

tabBarCont.viewControllers = [firstVc]


This will create a tab when it loads like I expect, so I know that works, and I can add secondVC etc to create more... What I'm struggling with is using my class number instead of firstVC - so I'd like to be able to generate them like this:



for class in classnumbers {

let class[0]Vc = UIViewController()
class[0]Vc.title = "class[0]"
class[0]Vc.tabBarItem = UITabBarItem.init(title: "class[0]", image: UIImage(named: "class[0]"), tag: 0)
tabBarCont.viewControllers = [class[0]Vc]

}


and so on for each class in the database. I've seen the question answered with "use an array", but I can't see how that would be any different to looping round my database results, and I've seen "you can't do that with swift" - so is it do-able or do I need to come up with a different way of looking at the problem? (I won't know the class numbers / names that a school use until after getting their data - if I have to hard code them, I'd have to recompile the app for every different school?)










share|improve this question























  • Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

    – Dom
    Nov 19 '18 at 22:48











  • I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

    – RobDag
    Nov 19 '18 at 22:54











  • Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

    – Myk
    Nov 20 '18 at 0:35











  • No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

    – RobDag
    Nov 20 '18 at 7:57











  • I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

    – Dom
    Nov 20 '18 at 15:48














1












1








1








I'm writing an app that needs to create a number of tabs based on values from a database. It gets given a list of Pupils and Class numbers from a CSV file, and imports them into a database. I can run a SELECT DISTINCT on the database and I get a list of class numbers (eg -2, -1, R, 1, 2) and I can iterate through the list fine using



for class in classnumbers {

print (class[0])

}


and see a list of my class numbers, but I'm banging my head against a brick wall trying to generate the tabs at runtime. I've got the code:



    let firstVc = UIViewController()
firstVc.title = "First"
firstVc.tabBarItem = UITabBarItem.init(title: "Home", image: UIImage(named: "HomeTab"), tag: 0)

tabBarCont.viewControllers = [firstVc]


This will create a tab when it loads like I expect, so I know that works, and I can add secondVC etc to create more... What I'm struggling with is using my class number instead of firstVC - so I'd like to be able to generate them like this:



for class in classnumbers {

let class[0]Vc = UIViewController()
class[0]Vc.title = "class[0]"
class[0]Vc.tabBarItem = UITabBarItem.init(title: "class[0]", image: UIImage(named: "class[0]"), tag: 0)
tabBarCont.viewControllers = [class[0]Vc]

}


and so on for each class in the database. I've seen the question answered with "use an array", but I can't see how that would be any different to looping round my database results, and I've seen "you can't do that with swift" - so is it do-able or do I need to come up with a different way of looking at the problem? (I won't know the class numbers / names that a school use until after getting their data - if I have to hard code them, I'd have to recompile the app for every different school?)










share|improve this question














I'm writing an app that needs to create a number of tabs based on values from a database. It gets given a list of Pupils and Class numbers from a CSV file, and imports them into a database. I can run a SELECT DISTINCT on the database and I get a list of class numbers (eg -2, -1, R, 1, 2) and I can iterate through the list fine using



for class in classnumbers {

print (class[0])

}


and see a list of my class numbers, but I'm banging my head against a brick wall trying to generate the tabs at runtime. I've got the code:



    let firstVc = UIViewController()
firstVc.title = "First"
firstVc.tabBarItem = UITabBarItem.init(title: "Home", image: UIImage(named: "HomeTab"), tag: 0)

tabBarCont.viewControllers = [firstVc]


This will create a tab when it loads like I expect, so I know that works, and I can add secondVC etc to create more... What I'm struggling with is using my class number instead of firstVC - so I'd like to be able to generate them like this:



for class in classnumbers {

let class[0]Vc = UIViewController()
class[0]Vc.title = "class[0]"
class[0]Vc.tabBarItem = UITabBarItem.init(title: "class[0]", image: UIImage(named: "class[0]"), tag: 0)
tabBarCont.viewControllers = [class[0]Vc]

}


and so on for each class in the database. I've seen the question answered with "use an array", but I can't see how that would be any different to looping round my database results, and I've seen "you can't do that with swift" - so is it do-able or do I need to come up with a different way of looking at the problem? (I won't know the class numbers / names that a school use until after getting their data - if I have to hard code them, I'd have to recompile the app for every different school?)







swift






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 22:45









RobDagRobDag

82




82













  • Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

    – Dom
    Nov 19 '18 at 22:48











  • I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

    – RobDag
    Nov 19 '18 at 22:54











  • Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

    – Myk
    Nov 20 '18 at 0:35











  • No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

    – RobDag
    Nov 20 '18 at 7:57











  • I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

    – Dom
    Nov 20 '18 at 15:48



















  • Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

    – Dom
    Nov 19 '18 at 22:48











  • I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

    – RobDag
    Nov 19 '18 at 22:54











  • Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

    – Myk
    Nov 20 '18 at 0:35











  • No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

    – RobDag
    Nov 20 '18 at 7:57











  • I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

    – Dom
    Nov 20 '18 at 15:48

















Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

– Dom
Nov 19 '18 at 22:48





Check out this tutorial and see if it helps medium.com/ios-os-x-development/…

– Dom
Nov 19 '18 at 22:48













I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

– RobDag
Nov 19 '18 at 22:54





I looked at that page earlier, and whilst he is creating the tabs programmatically rather than through Storyboard, he inputs all the names manually - I need to do it from a list rather than hand coding them in.

– RobDag
Nov 19 '18 at 22:54













Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

– Myk
Nov 20 '18 at 0:35





Is class your own variable? Or is this some swift magic I've never heard of using class keyword?

– Myk
Nov 20 '18 at 0:35













No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

– RobDag
Nov 20 '18 at 7:57





No - it’s a variable name - and looking at it fresh, maybe registrationGroup would have been a better one... but then I’d still need to create the regGroup[0]Vc etc

– RobDag
Nov 20 '18 at 7:57













I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

– Dom
Nov 20 '18 at 15:48





I'm confused what you're accessing with class[0]? Your first for-loop at the top you're using a for-each loop and access the class index 0? What is this value?

– Dom
Nov 20 '18 at 15:48












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383731%2fcreating-uitabbaritem-from-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383731%2fcreating-uitabbaritem-from-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings