Custom delegate nil and not saving userDefaults
I make a delegate inside UIView to passed data to my controller and than I save it in userdefaults. but when I try to save it, it return nil. How can I save it and not return nil.
this is my code in UIView and my protocol
protocol SaveUpdateInputDelegate {
func saveInputText()
func fetchInputText()
}
class FormUIView: UIView {
var delegate: SaveUpdateInputDelegate?
let nameLabel = ProfileCustomLabel(textName: "Name")
let nameTextField = CustomTextField(placeholderName: "Name")
let emailLabel = ProfileCustomLabel(textName: "Email")
let emailTextField = CustomTextField(placeholderName: "Email")
let titleLabel = ProfileCustomLabel(textName: "Title")
let titleTextField = CustomTextField(placeholderName: "Title")
let locationLabel = ProfileCustomLabel(textName: "Location")
let locationTextField = CustomTextField(placeholderName: "Location")
this is my controller code that have a button to save the input text and save it in userdefaults
class ProfileController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SaveUpdateInputDelegate {
// I need to initialized this to create autolayout
let formView: FormUIView = FormUIView()
let saveButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Save", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = #colorLiteral(red: 0.1254901961, green: 0.7411764706, blue: 1, alpha: 1)
btn.layer.cornerRadius = 18
btn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
handleSave()
}
@objc func handleSave() {
print("saved...")
guard let data = UserDefaults.standard.object(forKey: "profileImageEditedKey") as? Data else { return }
let image = UIImage.init(data: data)
DispatchQueue.main.async {
self.profileImageView.image = image?.withRenderingMode(.alwaysOriginal)
}
saveInputText()
fetchInputText()
}
func saveInputText() {
UserDefaults.standard.set(formView.nameTextField.text!, forKey: "nameTextFieldKey")
UserDefaults.standard.set(formView.emailTextField.text!, forKey: "emailTextFieldKey")
UserDefaults.standard.set(formView.titleTextField.text!, forKey: "titleTextFieldKey")
UserDefaults.standard.set(formView.locationTextField.text!, forKey: "locationTextFieldKey")
UserDefaults.standard.synchronize()
}
func fetchInputText() {
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
let email = UserDefaults.standard.value(forKey: "emailTextFieldKey") as? String ?? ""
let title = UserDefaults.standard.value(forKey: "titleTextFieldKey") as? String ?? ""
let location = UserDefaults.standard.value(forKey: "locationTextFieldKey") as? String ?? ""
formView.nameTextField.text = name
formView.emailTextField.text = email
formView.titleTextField.text = title
formView.locationTextField.text = location
}
ios swift delegates uitextfield
add a comment |
I make a delegate inside UIView to passed data to my controller and than I save it in userdefaults. but when I try to save it, it return nil. How can I save it and not return nil.
this is my code in UIView and my protocol
protocol SaveUpdateInputDelegate {
func saveInputText()
func fetchInputText()
}
class FormUIView: UIView {
var delegate: SaveUpdateInputDelegate?
let nameLabel = ProfileCustomLabel(textName: "Name")
let nameTextField = CustomTextField(placeholderName: "Name")
let emailLabel = ProfileCustomLabel(textName: "Email")
let emailTextField = CustomTextField(placeholderName: "Email")
let titleLabel = ProfileCustomLabel(textName: "Title")
let titleTextField = CustomTextField(placeholderName: "Title")
let locationLabel = ProfileCustomLabel(textName: "Location")
let locationTextField = CustomTextField(placeholderName: "Location")
this is my controller code that have a button to save the input text and save it in userdefaults
class ProfileController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SaveUpdateInputDelegate {
// I need to initialized this to create autolayout
let formView: FormUIView = FormUIView()
let saveButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Save", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = #colorLiteral(red: 0.1254901961, green: 0.7411764706, blue: 1, alpha: 1)
btn.layer.cornerRadius = 18
btn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
handleSave()
}
@objc func handleSave() {
print("saved...")
guard let data = UserDefaults.standard.object(forKey: "profileImageEditedKey") as? Data else { return }
let image = UIImage.init(data: data)
DispatchQueue.main.async {
self.profileImageView.image = image?.withRenderingMode(.alwaysOriginal)
}
saveInputText()
fetchInputText()
}
func saveInputText() {
UserDefaults.standard.set(formView.nameTextField.text!, forKey: "nameTextFieldKey")
UserDefaults.standard.set(formView.emailTextField.text!, forKey: "emailTextFieldKey")
UserDefaults.standard.set(formView.titleTextField.text!, forKey: "titleTextFieldKey")
UserDefaults.standard.set(formView.locationTextField.text!, forKey: "locationTextFieldKey")
UserDefaults.standard.synchronize()
}
func fetchInputText() {
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
let email = UserDefaults.standard.value(forKey: "emailTextFieldKey") as? String ?? ""
let title = UserDefaults.standard.value(forKey: "titleTextFieldKey") as? String ?? ""
let location = UserDefaults.standard.value(forKey: "locationTextFieldKey") as? String ?? ""
formView.nameTextField.text = name
formView.emailTextField.text = email
formView.titleTextField.text = title
formView.locationTextField.text = location
}
ios swift delegates uitextfield
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
If you callsaveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults
– jms
Jan 2 at 6:03
Can you put more code related to how you calledsaveInputText
andfetchInputText
?
– Kamran
Jan 2 at 6:24
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51
add a comment |
I make a delegate inside UIView to passed data to my controller and than I save it in userdefaults. but when I try to save it, it return nil. How can I save it and not return nil.
this is my code in UIView and my protocol
protocol SaveUpdateInputDelegate {
func saveInputText()
func fetchInputText()
}
class FormUIView: UIView {
var delegate: SaveUpdateInputDelegate?
let nameLabel = ProfileCustomLabel(textName: "Name")
let nameTextField = CustomTextField(placeholderName: "Name")
let emailLabel = ProfileCustomLabel(textName: "Email")
let emailTextField = CustomTextField(placeholderName: "Email")
let titleLabel = ProfileCustomLabel(textName: "Title")
let titleTextField = CustomTextField(placeholderName: "Title")
let locationLabel = ProfileCustomLabel(textName: "Location")
let locationTextField = CustomTextField(placeholderName: "Location")
this is my controller code that have a button to save the input text and save it in userdefaults
class ProfileController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SaveUpdateInputDelegate {
// I need to initialized this to create autolayout
let formView: FormUIView = FormUIView()
let saveButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Save", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = #colorLiteral(red: 0.1254901961, green: 0.7411764706, blue: 1, alpha: 1)
btn.layer.cornerRadius = 18
btn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
handleSave()
}
@objc func handleSave() {
print("saved...")
guard let data = UserDefaults.standard.object(forKey: "profileImageEditedKey") as? Data else { return }
let image = UIImage.init(data: data)
DispatchQueue.main.async {
self.profileImageView.image = image?.withRenderingMode(.alwaysOriginal)
}
saveInputText()
fetchInputText()
}
func saveInputText() {
UserDefaults.standard.set(formView.nameTextField.text!, forKey: "nameTextFieldKey")
UserDefaults.standard.set(formView.emailTextField.text!, forKey: "emailTextFieldKey")
UserDefaults.standard.set(formView.titleTextField.text!, forKey: "titleTextFieldKey")
UserDefaults.standard.set(formView.locationTextField.text!, forKey: "locationTextFieldKey")
UserDefaults.standard.synchronize()
}
func fetchInputText() {
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
let email = UserDefaults.standard.value(forKey: "emailTextFieldKey") as? String ?? ""
let title = UserDefaults.standard.value(forKey: "titleTextFieldKey") as? String ?? ""
let location = UserDefaults.standard.value(forKey: "locationTextFieldKey") as? String ?? ""
formView.nameTextField.text = name
formView.emailTextField.text = email
formView.titleTextField.text = title
formView.locationTextField.text = location
}
ios swift delegates uitextfield
I make a delegate inside UIView to passed data to my controller and than I save it in userdefaults. but when I try to save it, it return nil. How can I save it and not return nil.
this is my code in UIView and my protocol
protocol SaveUpdateInputDelegate {
func saveInputText()
func fetchInputText()
}
class FormUIView: UIView {
var delegate: SaveUpdateInputDelegate?
let nameLabel = ProfileCustomLabel(textName: "Name")
let nameTextField = CustomTextField(placeholderName: "Name")
let emailLabel = ProfileCustomLabel(textName: "Email")
let emailTextField = CustomTextField(placeholderName: "Email")
let titleLabel = ProfileCustomLabel(textName: "Title")
let titleTextField = CustomTextField(placeholderName: "Title")
let locationLabel = ProfileCustomLabel(textName: "Location")
let locationTextField = CustomTextField(placeholderName: "Location")
this is my controller code that have a button to save the input text and save it in userdefaults
class ProfileController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, SaveUpdateInputDelegate {
// I need to initialized this to create autolayout
let formView: FormUIView = FormUIView()
let saveButton: UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Save", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.backgroundColor = #colorLiteral(red: 0.1254901961, green: 0.7411764706, blue: 1, alpha: 1)
btn.layer.cornerRadius = 18
btn.addTarget(self, action: #selector(handleSave), for: .touchUpInside)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
handleSave()
}
@objc func handleSave() {
print("saved...")
guard let data = UserDefaults.standard.object(forKey: "profileImageEditedKey") as? Data else { return }
let image = UIImage.init(data: data)
DispatchQueue.main.async {
self.profileImageView.image = image?.withRenderingMode(.alwaysOriginal)
}
saveInputText()
fetchInputText()
}
func saveInputText() {
UserDefaults.standard.set(formView.nameTextField.text!, forKey: "nameTextFieldKey")
UserDefaults.standard.set(formView.emailTextField.text!, forKey: "emailTextFieldKey")
UserDefaults.standard.set(formView.titleTextField.text!, forKey: "titleTextFieldKey")
UserDefaults.standard.set(formView.locationTextField.text!, forKey: "locationTextFieldKey")
UserDefaults.standard.synchronize()
}
func fetchInputText() {
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
let email = UserDefaults.standard.value(forKey: "emailTextFieldKey") as? String ?? ""
let title = UserDefaults.standard.value(forKey: "titleTextFieldKey") as? String ?? ""
let location = UserDefaults.standard.value(forKey: "locationTextFieldKey") as? String ?? ""
formView.nameTextField.text = name
formView.emailTextField.text = email
formView.titleTextField.text = title
formView.locationTextField.text = location
}
ios swift delegates uitextfield
ios swift delegates uitextfield
edited Jan 2 at 6:50
ferryawijayanto
asked Jan 2 at 4:46
ferryawijayantoferryawijayanto
1008
1008
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
If you callsaveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults
– jms
Jan 2 at 6:03
Can you put more code related to how you calledsaveInputText
andfetchInputText
?
– Kamran
Jan 2 at 6:24
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51
add a comment |
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
If you callsaveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults
– jms
Jan 2 at 6:03
Can you put more code related to how you calledsaveInputText
andfetchInputText
?
– Kamran
Jan 2 at 6:24
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
If you call
saveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults– jms
Jan 2 at 6:03
If you call
saveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults– jms
Jan 2 at 6:03
Can you put more code related to how you called
saveInputText
and fetchInputText
?– Kamran
Jan 2 at 6:24
Can you put more code related to how you called
saveInputText
and fetchInputText
?– Kamran
Jan 2 at 6:24
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51
add a comment |
3 Answers
3
active
oldest
votes
You should call only fetchInputText
in viewDidLoad
just to set the textFields
text from the UserDefaults
. Currently you are calling handleSave
which internally calls saveInputText
when textFields
have no values(or default values). It might crash if you haven't put any default text
in Storyboard/Xib
for any of these textFields
.,
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
fetchInputText()
}
And the purpose of using delegate
does not look meaningful because the FormUIView
is not propagating anything for delegate
to handle. This might be wrong as i don't know the complete implementation of FormUIView
.
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
add a comment |
I don't understand why you are using delegate to pass data that is not necessary or you are doing something wrong.
As all are the textfield so you can directly set textField Delegate to ViewController So there is no need for passing value around & this is not proper use of delegate here
Suppose you can't do that then you need to call delegate every time when textFieldShouldChangeCharacter
or textFieldDidEndEditing
Called but as I said this is wrong way
Step1: Verify all correct information is saved inside
Step 2: UserDefault has already method to fetch string
Replace this
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
with
let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""
and others too.
where the user called theformView.saveInputText()
function in class
– Anbu.Karthik
Jan 2 at 4:53
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
|
show 3 more comments
Call formView.saveInputText()
should be called from viewDidAppear()
function instead of viewDidLoad()
function.
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/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
});
}
});
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%2f54001309%2fcustom-delegate-nil-and-not-saving-userdefaults%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should call only fetchInputText
in viewDidLoad
just to set the textFields
text from the UserDefaults
. Currently you are calling handleSave
which internally calls saveInputText
when textFields
have no values(or default values). It might crash if you haven't put any default text
in Storyboard/Xib
for any of these textFields
.,
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
fetchInputText()
}
And the purpose of using delegate
does not look meaningful because the FormUIView
is not propagating anything for delegate
to handle. This might be wrong as i don't know the complete implementation of FormUIView
.
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
add a comment |
You should call only fetchInputText
in viewDidLoad
just to set the textFields
text from the UserDefaults
. Currently you are calling handleSave
which internally calls saveInputText
when textFields
have no values(or default values). It might crash if you haven't put any default text
in Storyboard/Xib
for any of these textFields
.,
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
fetchInputText()
}
And the purpose of using delegate
does not look meaningful because the FormUIView
is not propagating anything for delegate
to handle. This might be wrong as i don't know the complete implementation of FormUIView
.
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
add a comment |
You should call only fetchInputText
in viewDidLoad
just to set the textFields
text from the UserDefaults
. Currently you are calling handleSave
which internally calls saveInputText
when textFields
have no values(or default values). It might crash if you haven't put any default text
in Storyboard/Xib
for any of these textFields
.,
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
fetchInputText()
}
And the purpose of using delegate
does not look meaningful because the FormUIView
is not propagating anything for delegate
to handle. This might be wrong as i don't know the complete implementation of FormUIView
.
You should call only fetchInputText
in viewDidLoad
just to set the textFields
text from the UserDefaults
. Currently you are calling handleSave
which internally calls saveInputText
when textFields
have no values(or default values). It might crash if you haven't put any default text
in Storyboard/Xib
for any of these textFields
.,
override func viewDidLoad() {
super.viewDidLoad()
formView.delegate = self
fetchInputText()
}
And the purpose of using delegate
does not look meaningful because the FormUIView
is not propagating anything for delegate
to handle. This might be wrong as i don't know the complete implementation of FormUIView
.
answered Jan 2 at 7:11


KamranKamran
6,95021129
6,95021129
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
add a comment |
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
hey thank you I don't know how but it works :) @Kamran
– ferryawijayanto
Jan 2 at 8:11
add a comment |
I don't understand why you are using delegate to pass data that is not necessary or you are doing something wrong.
As all are the textfield so you can directly set textField Delegate to ViewController So there is no need for passing value around & this is not proper use of delegate here
Suppose you can't do that then you need to call delegate every time when textFieldShouldChangeCharacter
or textFieldDidEndEditing
Called but as I said this is wrong way
Step1: Verify all correct information is saved inside
Step 2: UserDefault has already method to fetch string
Replace this
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
with
let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""
and others too.
where the user called theformView.saveInputText()
function in class
– Anbu.Karthik
Jan 2 at 4:53
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
|
show 3 more comments
I don't understand why you are using delegate to pass data that is not necessary or you are doing something wrong.
As all are the textfield so you can directly set textField Delegate to ViewController So there is no need for passing value around & this is not proper use of delegate here
Suppose you can't do that then you need to call delegate every time when textFieldShouldChangeCharacter
or textFieldDidEndEditing
Called but as I said this is wrong way
Step1: Verify all correct information is saved inside
Step 2: UserDefault has already method to fetch string
Replace this
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
with
let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""
and others too.
where the user called theformView.saveInputText()
function in class
– Anbu.Karthik
Jan 2 at 4:53
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
|
show 3 more comments
I don't understand why you are using delegate to pass data that is not necessary or you are doing something wrong.
As all are the textfield so you can directly set textField Delegate to ViewController So there is no need for passing value around & this is not proper use of delegate here
Suppose you can't do that then you need to call delegate every time when textFieldShouldChangeCharacter
or textFieldDidEndEditing
Called but as I said this is wrong way
Step1: Verify all correct information is saved inside
Step 2: UserDefault has already method to fetch string
Replace this
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
with
let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""
and others too.
I don't understand why you are using delegate to pass data that is not necessary or you are doing something wrong.
As all are the textfield so you can directly set textField Delegate to ViewController So there is no need for passing value around & this is not proper use of delegate here
Suppose you can't do that then you need to call delegate every time when textFieldShouldChangeCharacter
or textFieldDidEndEditing
Called but as I said this is wrong way
Step1: Verify all correct information is saved inside
Step 2: UserDefault has already method to fetch string
Replace this
let name = UserDefaults.standard.value(forKey: "nameTextFieldKey") as? String ?? ""
with
let name = UserDefaults.standard.string(forKey: "nameTextFieldKey") ?? ""
and others too.
edited Jan 2 at 5:42
answered Jan 2 at 4:51
Prashant TukadiyaPrashant Tukadiya
7,69622450
7,69622450
where the user called theformView.saveInputText()
function in class
– Anbu.Karthik
Jan 2 at 4:53
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
|
show 3 more comments
where the user called theformView.saveInputText()
function in class
– Anbu.Karthik
Jan 2 at 4:53
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
where the user called the
formView.saveInputText()
function in class– Anbu.Karthik
Jan 2 at 4:53
where the user called the
formView.saveInputText()
function in class– Anbu.Karthik
Jan 2 at 4:53
1
1
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik That's the requirement of user. Without seeing all scenario it is difficult to tell. However he doesn't need that. As all are textfield he can directly set delegate of textfield to viewcontroller
– Prashant Tukadiya
Jan 2 at 4:55
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
@Anbu.Karthik Edited my answer
– Prashant Tukadiya
Jan 2 at 4:59
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
so I only save it in userdefaults once in 'textFieldShouldChangeCharacter'? and retrieve it In that too @PrashantTukadiya
– ferryawijayanto
Jan 2 at 5:39
1
1
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
@ferryawijayanto Your intension is right no doubt. but method you choose is seems to be problematic in case if your app grows in future. What I suggest you is that it is fine to keep your label & textfield to Uiview Subclass but passing data around makes no sense as Texteild is anyway part of UIViewController and you can directly access with self. like self.yourCusotmView.textfield. Solution would be keep your textField delegate to your view controller like self.yourView.txtField1.delegate = self. so you have more control over data.
– Prashant Tukadiya
Jan 2 at 5:55
|
show 3 more comments
Call formView.saveInputText()
should be called from viewDidAppear()
function instead of viewDidLoad()
function.
add a comment |
Call formView.saveInputText()
should be called from viewDidAppear()
function instead of viewDidLoad()
function.
add a comment |
Call formView.saveInputText()
should be called from viewDidAppear()
function instead of viewDidLoad()
function.
Call formView.saveInputText()
should be called from viewDidAppear()
function instead of viewDidLoad()
function.
answered Jan 2 at 6:52
ShwetaShweta
133
133
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%2f54001309%2fcustom-delegate-nil-and-not-saving-userdefaults%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
can you put code of calling "saveInputText" function?
– Jatin Kathrotiya
Jan 2 at 4:55
I put it in viewDidLoad @JatinKathrotiya
– ferryawijayanto
Jan 2 at 5:10
If you call
saveInputText()
in viewDidLoad(), surely the textfields are empty and there won't be any saved in UserDefaults– jms
Jan 2 at 6:03
Can you put more code related to how you called
saveInputText
andfetchInputText
?– Kamran
Jan 2 at 6:24
@Kamran I update my question check it out
– ferryawijayanto
Jan 2 at 6:51