how to init() a swift view controller properly?How to write init methods of a UIViewController in SwiftHow do I call Objective-C code from Swift?#pragma mark in Swift?Swift Beta performance: sorting arraysHow to detect tableView cell touched or clicked in swiftAVaudioplayer audio player progess as Uislider crashesAdding a custom UIViewcontroller to subview programmatically but getting an error message “Cannot convert value of type…”How to optimize UITableViewCell, because my UITableView lagsHow do I pass data from a UIViewController to UITabBarController?Swift Error - Use of undeclared type 'cell' - Collection ViewBMI app print result is 0 even with variable being hard coded
count network interfaces in bash
How do I find the unknown program enabled during Start-Up?
Translation Golf XLIX - An Accurate Shot
How much money would I need to feel secure in my job?
Bash to check if directory exist. If not create with an array
Is there a way to realize a function of type ((a -> b) -> b) -> Either a b?
Was there a clearly identifiable "first computer" to use or demonstrate the use of virtual memory?
Polling on Impeachment
Why are Democrats mostly focused on increasing healthcare spending, rarely mentioning any proposals for decreasing the costs of healthcare services?
Why does Smaug have 4 legs in the 1st movie but only 2 legs in the 2nd?
How to present boolean options along with selecting exactly 1 of them as "primary"?
Speaking German abroad and feeling condescended to when people speak English back to me
Is it a good idea to contact a candidate?
How to interpret Residuals vs. Fitted Plot
Making Sandwiches
What are these criss-cross patterns close to Cambridge Airport (UK)?
"Ich habe Durst" vs "Ich bin durstig": Which is more common?
How to make sure there's equal difference in length between lines
Can you make monkeys human?
Does SQL Server Only Perform Calculations In A SELECT List Once?
What is the meaning of Text inside of AMS logo
SD Card speed degrading and doesn't work on one of my cameras: can I do something?
Decision problems for which it is unknown whether they are decidable
Polynomial Chebyshev Regression versus multi-linear regression
how to init() a swift view controller properly?
How to write init methods of a UIViewController in SwiftHow do I call Objective-C code from Swift?#pragma mark in Swift?Swift Beta performance: sorting arraysHow to detect tableView cell touched or clicked in swiftAVaudioplayer audio player progess as Uislider crashesAdding a custom UIViewcontroller to subview programmatically but getting an error message “Cannot convert value of type…”How to optimize UITableViewCell, because my UITableView lagsHow do I pass data from a UIViewController to UITabBarController?Swift Error - Use of undeclared type 'cell' - Collection ViewBMI app print result is 0 even with variable being hard coded
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).
How do I initialize this class properly?
Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBOutlet weak var answerLabel: UILabel!
//global vars for all funcs
var selection1: Int?
didSet answerLabel.text = String(selection1!)
var selection2: String?
didSet answerLabel.text = selection2!
var selection3: Int?
didSet answerLabel.text = String(selection3!)
var answer: Int
didSet answerLabel.text = String(answer)
init()
@IBAction func touchButton(_ sender: UIButton)
if selection1 == nil
selection1 = Int(sender.currentTitle!)
print("Selection set in first pos.")
else if selection2 == nil
selection2 = sender.currentTitle
else if selection3 == nil
selection3 = Int(sender.currentTitle!)
else
calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
func calculate(firstNum: Int, operation: String, secondNum: Int)
switch operation
case "+":
answer = firstNum + secondNum
case "-":
answer = firstNum - secondNum
case "x":
answer = firstNum * secondNum
case "/":
answer = firstNum / secondNum
default:
answerLabel.text = "Something went wrong!"
swift initialization
add a comment
|
I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).
How do I initialize this class properly?
Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBOutlet weak var answerLabel: UILabel!
//global vars for all funcs
var selection1: Int?
didSet answerLabel.text = String(selection1!)
var selection2: String?
didSet answerLabel.text = selection2!
var selection3: Int?
didSet answerLabel.text = String(selection3!)
var answer: Int
didSet answerLabel.text = String(answer)
init()
@IBAction func touchButton(_ sender: UIButton)
if selection1 == nil
selection1 = Int(sender.currentTitle!)
print("Selection set in first pos.")
else if selection2 == nil
selection2 = sender.currentTitle
else if selection3 == nil
selection3 = Int(sender.currentTitle!)
else
calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
func calculate(firstNum: Int, operation: String, secondNum: Int)
switch operation
case "+":
answer = firstNum + secondNum
case "-":
answer = firstNum - secondNum
case "x":
answer = firstNum * secondNum
case "/":
answer = firstNum / secondNum
default:
answerLabel.text = "Something went wrong!"
swift initialization
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24
add a comment
|
I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).
How do I initialize this class properly?
Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBOutlet weak var answerLabel: UILabel!
//global vars for all funcs
var selection1: Int?
didSet answerLabel.text = String(selection1!)
var selection2: String?
didSet answerLabel.text = selection2!
var selection3: Int?
didSet answerLabel.text = String(selection3!)
var answer: Int
didSet answerLabel.text = String(answer)
init()
@IBAction func touchButton(_ sender: UIButton)
if selection1 == nil
selection1 = Int(sender.currentTitle!)
print("Selection set in first pos.")
else if selection2 == nil
selection2 = sender.currentTitle
else if selection3 == nil
selection3 = Int(sender.currentTitle!)
else
calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
func calculate(firstNum: Int, operation: String, secondNum: Int)
switch operation
case "+":
answer = firstNum + secondNum
case "-":
answer = firstNum - secondNum
case "x":
answer = firstNum * secondNum
case "/":
answer = firstNum / secondNum
default:
answerLabel.text = "Something went wrong!"
swift initialization
I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).
How do I initialize this class properly?
Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBOutlet weak var answerLabel: UILabel!
//global vars for all funcs
var selection1: Int?
didSet answerLabel.text = String(selection1!)
var selection2: String?
didSet answerLabel.text = selection2!
var selection3: Int?
didSet answerLabel.text = String(selection3!)
var answer: Int
didSet answerLabel.text = String(answer)
init()
@IBAction func touchButton(_ sender: UIButton)
if selection1 == nil
selection1 = Int(sender.currentTitle!)
print("Selection set in first pos.")
else if selection2 == nil
selection2 = sender.currentTitle
else if selection3 == nil
selection3 = Int(sender.currentTitle!)
else
calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
func calculate(firstNum: Int, operation: String, secondNum: Int)
switch operation
case "+":
answer = firstNum + secondNum
case "-":
answer = firstNum - secondNum
case "x":
answer = firstNum * secondNum
case "/":
answer = firstNum / secondNum
default:
answerLabel.text = "Something went wrong!"
swift initialization
swift initialization
edited Mar 28 at 22:01
asked Mar 28 at 21:53
user11274697user11274697
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24
add a comment
|
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24
add a comment
|
2 Answers
2
active
oldest
votes
Initialization depends on a couple of condition.
- If you are using storyboard, you can just remove the
init
and your VC will have default initializer. Make sure either all of your properties have default value or they are optional. - If you are using
xib
or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController
var answer: Int
convenience init(answer: Int)
self.init()
self.answer = answer
// Do other setup
add a comment
|
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad
, ie:
override func viewDidLoad()
super.viewDidLoad()
// configure your views and subviews here
add a comment
|
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/4.0/"u003ecc by-sa 4.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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407422%2fhow-to-init-a-swift-view-controller-properly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Initialization depends on a couple of condition.
- If you are using storyboard, you can just remove the
init
and your VC will have default initializer. Make sure either all of your properties have default value or they are optional. - If you are using
xib
or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController
var answer: Int
convenience init(answer: Int)
self.init()
self.answer = answer
// Do other setup
add a comment
|
Initialization depends on a couple of condition.
- If you are using storyboard, you can just remove the
init
and your VC will have default initializer. Make sure either all of your properties have default value or they are optional. - If you are using
xib
or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController
var answer: Int
convenience init(answer: Int)
self.init()
self.answer = answer
// Do other setup
add a comment
|
Initialization depends on a couple of condition.
- If you are using storyboard, you can just remove the
init
and your VC will have default initializer. Make sure either all of your properties have default value or they are optional. - If you are using
xib
or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController
var answer: Int
convenience init(answer: Int)
self.init()
self.answer = answer
// Do other setup
Initialization depends on a couple of condition.
- If you are using storyboard, you can just remove the
init
and your VC will have default initializer. Make sure either all of your properties have default value or they are optional. - If you are using
xib
or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController
var answer: Int
convenience init(answer: Int)
self.init()
self.answer = answer
// Do other setup
answered Mar 29 at 4:59
AksAks
1,23511 silver badges22 bronze badges
1,23511 silver badges22 bronze badges
add a comment
|
add a comment
|
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad
, ie:
override func viewDidLoad()
super.viewDidLoad()
// configure your views and subviews here
add a comment
|
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad
, ie:
override func viewDidLoad()
super.viewDidLoad()
// configure your views and subviews here
add a comment
|
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad
, ie:
override func viewDidLoad()
super.viewDidLoad()
// configure your views and subviews here
Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad
, ie:
override func viewDidLoad()
super.viewDidLoad()
// configure your views and subviews here
answered Mar 29 at 0:54
eeekarieeekari
1441 silver badge6 bronze badges
1441 silver badge6 bronze badges
add a comment
|
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55407422%2fhow-to-init-a-swift-view-controller-properly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
because I am using optionals where the vars may not contain a value, i am required to init the class somehow
– user11274697
Mar 28 at 22:19
Duplicate -- stackoverflow.com/questions/30679129/…
– Mocha
Mar 28 at 22:25
You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 ...
– Mocha
Mar 28 at 23:24