Listening for input in programmatically instantiated xib templates
I am currently messing around with programmatically adding and removing template Views; to remove a UIView template, a user has to tap a button within it.
However, I am not sure how to get the parent view controller to handle button inputs. I would like the parent view controller to know when the button is pressed instead of having the xib class handle that because otherwise I would have to a lot of circling and it would get annoying really fast.
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime.
How do I get a parent ViewController to handle the UIButton inputs within a custom XIB template?
ios uibutton xib
add a comment |
I am currently messing around with programmatically adding and removing template Views; to remove a UIView template, a user has to tap a button within it.
However, I am not sure how to get the parent view controller to handle button inputs. I would like the parent view controller to know when the button is pressed instead of having the xib class handle that because otherwise I would have to a lot of circling and it would get annoying really fast.
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime.
How do I get a parent ViewController to handle the UIButton inputs within a custom XIB template?
ios uibutton xib
add a comment |
I am currently messing around with programmatically adding and removing template Views; to remove a UIView template, a user has to tap a button within it.
However, I am not sure how to get the parent view controller to handle button inputs. I would like the parent view controller to know when the button is pressed instead of having the xib class handle that because otherwise I would have to a lot of circling and it would get annoying really fast.
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime.
How do I get a parent ViewController to handle the UIButton inputs within a custom XIB template?
ios uibutton xib
I am currently messing around with programmatically adding and removing template Views; to remove a UIView template, a user has to tap a button within it.
However, I am not sure how to get the parent view controller to handle button inputs. I would like the parent view controller to know when the button is pressed instead of having the xib class handle that because otherwise I would have to a lot of circling and it would get annoying really fast.
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime.
How do I get a parent ViewController to handle the UIButton inputs within a custom XIB template?
ios uibutton xib
ios uibutton xib
asked Nov 21 '18 at 0:03
TakeMeHomeCountryRoads
2716
2716
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime
That's not true, because you can certainly call addTarget(_:action:for:)
to connect the button action to the view controller target at the time you "add the xib."
However, an even cooler solution would be configure your button in the xib with a nil-targeted action. This means we don't need an explicit target; we simply have to know the name of the method that the action will call. The view controller is higher up the responder chain than the button (once you "add the xib" to view controller's view), and so when the button is tapped, the method will be found dynamically by walking up the responder chain.
add a comment |
you can use the method
button.addTarget(self, action: #selector(yourMethodName), for: .touchUpInside)
here the "button" will be the reference of the button in your xib. "self" will be the reference of the viewController in which you are adding the .xib.
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%2f53403446%2flistening-for-input-in-programmatically-instantiated-xib-templates%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
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime
That's not true, because you can certainly call addTarget(_:action:for:)
to connect the button action to the view controller target at the time you "add the xib."
However, an even cooler solution would be configure your button in the xib with a nil-targeted action. This means we don't need an explicit target; we simply have to know the name of the method that the action will call. The view controller is higher up the responder chain than the button (once you "add the xib" to view controller's view), and so when the button is tapped, the method will be found dynamically by walking up the responder chain.
add a comment |
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime
That's not true, because you can certainly call addTarget(_:action:for:)
to connect the button action to the view controller target at the time you "add the xib."
However, an even cooler solution would be configure your button in the xib with a nil-targeted action. This means we don't need an explicit target; we simply have to know the name of the method that the action will call. The view controller is higher up the responder chain than the button (once you "add the xib" to view controller's view), and so when the button is tapped, the method will be found dynamically by walking up the responder chain.
add a comment |
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime
That's not true, because you can certainly call addTarget(_:action:for:)
to connect the button action to the view controller target at the time you "add the xib."
However, an even cooler solution would be configure your button in the xib with a nil-targeted action. This means we don't need an explicit target; we simply have to know the name of the method that the action will call. The view controller is higher up the responder chain than the button (once you "add the xib" to view controller's view), and so when the button is tapped, the method will be found dynamically by walking up the responder chain.
Likewise, I cannot manually connect the button itself to the ViewController class because these xibs are added in during runtime
That's not true, because you can certainly call addTarget(_:action:for:)
to connect the button action to the view controller target at the time you "add the xib."
However, an even cooler solution would be configure your button in the xib with a nil-targeted action. This means we don't need an explicit target; we simply have to know the name of the method that the action will call. The view controller is higher up the responder chain than the button (once you "add the xib" to view controller's view), and so when the button is tapped, the method will be found dynamically by walking up the responder chain.
answered Nov 21 '18 at 1:02
matt
324k45522722
324k45522722
add a comment |
add a comment |
you can use the method
button.addTarget(self, action: #selector(yourMethodName), for: .touchUpInside)
here the "button" will be the reference of the button in your xib. "self" will be the reference of the viewController in which you are adding the .xib.
add a comment |
you can use the method
button.addTarget(self, action: #selector(yourMethodName), for: .touchUpInside)
here the "button" will be the reference of the button in your xib. "self" will be the reference of the viewController in which you are adding the .xib.
add a comment |
you can use the method
button.addTarget(self, action: #selector(yourMethodName), for: .touchUpInside)
here the "button" will be the reference of the button in your xib. "self" will be the reference of the viewController in which you are adding the .xib.
you can use the method
button.addTarget(self, action: #selector(yourMethodName), for: .touchUpInside)
here the "button" will be the reference of the button in your xib. "self" will be the reference of the viewController in which you are adding the .xib.
answered Nov 21 '18 at 1:13
Karthick Ramesh
753822
753822
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53403446%2flistening-for-input-in-programmatically-instantiated-xib-templates%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