How to access UIView in ViewController when calling function from another file?
Situation: I paint stuff through a button-click on a subview. Then I add the subview to the view.
Problem: When calling drawNew
from another swift file, the if-loop
is not triggered. Probably because the view
with the tag 777
does not get found.
Question: How do I tell the code to look for the view
in ViewController?
When do I call the function drawNew()
? I call it from another file with override func touchesEnded(...)
//###########################
// ViewController.swift
//###########################
var dv = Painting() // in here are functions to create a cg path
//
// IBActions
//
@IBAction func buttonPressed(_ sender: UIButton) {
// painting
if sender.tag == 1 {
drawNew()
} else if sender.tag == 2 {
CanvasView.clearCanvas()
}
}
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = view.viewWithTag(777) { // problem lies here maybe
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// prepare the canvas for
let newView = CharacterView(frame: CGRect(x: 0,
y:0,
width: HandwritingCanvasView.frame.width,
height: HandwritingCanvasView.frame.height))
newView.tag = 777
HandwritingCanvasView.addSubview(newView)
// self.view.addSubview(demoView)
}
Then this is the other file from where I call the drawNew()
func
// HandwritingCanvasView.swift
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
ViewController.drawNew()
}
ios swift uiview
add a comment |
Situation: I paint stuff through a button-click on a subview. Then I add the subview to the view.
Problem: When calling drawNew
from another swift file, the if-loop
is not triggered. Probably because the view
with the tag 777
does not get found.
Question: How do I tell the code to look for the view
in ViewController?
When do I call the function drawNew()
? I call it from another file with override func touchesEnded(...)
//###########################
// ViewController.swift
//###########################
var dv = Painting() // in here are functions to create a cg path
//
// IBActions
//
@IBAction func buttonPressed(_ sender: UIButton) {
// painting
if sender.tag == 1 {
drawNew()
} else if sender.tag == 2 {
CanvasView.clearCanvas()
}
}
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = view.viewWithTag(777) { // problem lies here maybe
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// prepare the canvas for
let newView = CharacterView(frame: CGRect(x: 0,
y:0,
width: HandwritingCanvasView.frame.width,
height: HandwritingCanvasView.frame.height))
newView.tag = 777
HandwritingCanvasView.addSubview(newView)
// self.view.addSubview(demoView)
}
Then this is the other file from where I call the drawNew()
func
// HandwritingCanvasView.swift
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
ViewController.drawNew()
}
ios swift uiview
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57
add a comment |
Situation: I paint stuff through a button-click on a subview. Then I add the subview to the view.
Problem: When calling drawNew
from another swift file, the if-loop
is not triggered. Probably because the view
with the tag 777
does not get found.
Question: How do I tell the code to look for the view
in ViewController?
When do I call the function drawNew()
? I call it from another file with override func touchesEnded(...)
//###########################
// ViewController.swift
//###########################
var dv = Painting() // in here are functions to create a cg path
//
// IBActions
//
@IBAction func buttonPressed(_ sender: UIButton) {
// painting
if sender.tag == 1 {
drawNew()
} else if sender.tag == 2 {
CanvasView.clearCanvas()
}
}
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = view.viewWithTag(777) { // problem lies here maybe
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// prepare the canvas for
let newView = CharacterView(frame: CGRect(x: 0,
y:0,
width: HandwritingCanvasView.frame.width,
height: HandwritingCanvasView.frame.height))
newView.tag = 777
HandwritingCanvasView.addSubview(newView)
// self.view.addSubview(demoView)
}
Then this is the other file from where I call the drawNew()
func
// HandwritingCanvasView.swift
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
ViewController.drawNew()
}
ios swift uiview
Situation: I paint stuff through a button-click on a subview. Then I add the subview to the view.
Problem: When calling drawNew
from another swift file, the if-loop
is not triggered. Probably because the view
with the tag 777
does not get found.
Question: How do I tell the code to look for the view
in ViewController?
When do I call the function drawNew()
? I call it from another file with override func touchesEnded(...)
//###########################
// ViewController.swift
//###########################
var dv = Painting() // in here are functions to create a cg path
//
// IBActions
//
@IBAction func buttonPressed(_ sender: UIButton) {
// painting
if sender.tag == 1 {
drawNew()
} else if sender.tag == 2 {
CanvasView.clearCanvas()
}
}
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = view.viewWithTag(777) { // problem lies here maybe
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// prepare the canvas for
let newView = CharacterView(frame: CGRect(x: 0,
y:0,
width: HandwritingCanvasView.frame.width,
height: HandwritingCanvasView.frame.height))
newView.tag = 777
HandwritingCanvasView.addSubview(newView)
// self.view.addSubview(demoView)
}
Then this is the other file from where I call the drawNew()
func
// HandwritingCanvasView.swift
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
ViewController.drawNew()
}
ios swift uiview
ios swift uiview
edited Nov 25 '18 at 14:46
ItsMeDom
asked Nov 25 '18 at 13:38
ItsMeDomItsMeDom
1842317
1842317
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57
add a comment |
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57
add a comment |
1 Answer
1
active
oldest
votes
It will be easier to have reference on newView in ViewController.
Try to do next
//###########################
// ViewController.swift
//###########################
weak var subView: UIView? //Add reference on your view
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = subView { // Use the reference instead of searching view with tag
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
// Your code is here
// ...
HandwritingCanvasView.addSubview(newView)
subView = newView
//...
}
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
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%2f53468032%2fhow-to-access-uiview-in-viewcontroller-when-calling-function-from-another-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It will be easier to have reference on newView in ViewController.
Try to do next
//###########################
// ViewController.swift
//###########################
weak var subView: UIView? //Add reference on your view
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = subView { // Use the reference instead of searching view with tag
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
// Your code is here
// ...
HandwritingCanvasView.addSubview(newView)
subView = newView
//...
}
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
add a comment |
It will be easier to have reference on newView in ViewController.
Try to do next
//###########################
// ViewController.swift
//###########################
weak var subView: UIView? //Add reference on your view
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = subView { // Use the reference instead of searching view with tag
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
// Your code is here
// ...
HandwritingCanvasView.addSubview(newView)
subView = newView
//...
}
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
add a comment |
It will be easier to have reference on newView in ViewController.
Try to do next
//###########################
// ViewController.swift
//###########################
weak var subView: UIView? //Add reference on your view
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = subView { // Use the reference instead of searching view with tag
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
// Your code is here
// ...
HandwritingCanvasView.addSubview(newView)
subView = newView
//...
}
It will be easier to have reference on newView in ViewController.
Try to do next
//###########################
// ViewController.swift
//###########################
weak var subView: UIView? //Add reference on your view
//
// FUNCTIONS
//
func drawNew() {
dv.makeMeSomePath()
if let foundView = subView { // Use the reference instead of searching view with tag
dv.tag = 111
foundView.addSubview(dv)
print("added subview")
}
}
override func viewDidAppear(_ animated: Bool) {
// Your code is here
// ...
HandwritingCanvasView.addSubview(newView)
subView = newView
//...
}
answered Nov 25 '18 at 15:18
GkoluniaGkolunia
828
828
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
add a comment |
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
Works, thanks! I also needed to declare ``let mainViewController = ViewController()´´ in the HandwritingCanvas Class.. Above the class...
– ItsMeDom
Nov 26 '18 at 6:34
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%2f53468032%2fhow-to-access-uiview-in-viewcontroller-when-calling-function-from-another-file%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
Needs more details, When do you add the view with tag 777? and on what event you call drawNew() from another swift file?
– Gkolunia
Nov 25 '18 at 14:25
@Gkolunia I added more code... Hope it helps...
– ItsMeDom
Nov 25 '18 at 14:47
And where do you set ViewController for HandwritingCanvasView?
– Gkolunia
Nov 25 '18 at 14:57