Performing Segue from within a View
I have a custom class named MenuBar
which is a UIView
used to present a UICollectionView
to a view controller. The problem I'm having is using a segue to another view controller when a UICollectionViewCell
is selected. Since MenuBar
is not a view controller, I can't seem to call performSegue()
. I created a segue in my storyboard between the two view controllers with an identifier. How can I properly trigger a segue from within my custom view class? How would this normally be done? I tried making a new instance of my view controller so that I could us the .performSegue()
from within the didSelectItemAt
collectionView
function but I don't think this is the proper way to do it at all. example:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController()
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Any suggestions on how to perform the segue from within this function would be greatly appreciated. Below I will supply the key components of my existing code so that you can see what I'm doing. There is a lot more code in both classes but I think this should be enough for the general idea of what I'm trying to accomplish.
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate {
override func viewDidLoad() {
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
private func setupCollectionView() {
self.addSubview(collectionView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController() // I want to perform a segue from here
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
}
Thanks in advance to anyone who can provide helpful advice and suggestions!
ios swift uicollectionview segue
add a comment |
I have a custom class named MenuBar
which is a UIView
used to present a UICollectionView
to a view controller. The problem I'm having is using a segue to another view controller when a UICollectionViewCell
is selected. Since MenuBar
is not a view controller, I can't seem to call performSegue()
. I created a segue in my storyboard between the two view controllers with an identifier. How can I properly trigger a segue from within my custom view class? How would this normally be done? I tried making a new instance of my view controller so that I could us the .performSegue()
from within the didSelectItemAt
collectionView
function but I don't think this is the proper way to do it at all. example:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController()
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Any suggestions on how to perform the segue from within this function would be greatly appreciated. Below I will supply the key components of my existing code so that you can see what I'm doing. There is a lot more code in both classes but I think this should be enough for the general idea of what I'm trying to accomplish.
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate {
override func viewDidLoad() {
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
private func setupCollectionView() {
self.addSubview(collectionView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController() // I want to perform a segue from here
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
}
Thanks in advance to anyone who can provide helpful advice and suggestions!
ios swift uicollectionview segue
add a comment |
I have a custom class named MenuBar
which is a UIView
used to present a UICollectionView
to a view controller. The problem I'm having is using a segue to another view controller when a UICollectionViewCell
is selected. Since MenuBar
is not a view controller, I can't seem to call performSegue()
. I created a segue in my storyboard between the two view controllers with an identifier. How can I properly trigger a segue from within my custom view class? How would this normally be done? I tried making a new instance of my view controller so that I could us the .performSegue()
from within the didSelectItemAt
collectionView
function but I don't think this is the proper way to do it at all. example:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController()
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Any suggestions on how to perform the segue from within this function would be greatly appreciated. Below I will supply the key components of my existing code so that you can see what I'm doing. There is a lot more code in both classes but I think this should be enough for the general idea of what I'm trying to accomplish.
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate {
override func viewDidLoad() {
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
private func setupCollectionView() {
self.addSubview(collectionView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController() // I want to perform a segue from here
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
}
Thanks in advance to anyone who can provide helpful advice and suggestions!
ios swift uicollectionview segue
I have a custom class named MenuBar
which is a UIView
used to present a UICollectionView
to a view controller. The problem I'm having is using a segue to another view controller when a UICollectionViewCell
is selected. Since MenuBar
is not a view controller, I can't seem to call performSegue()
. I created a segue in my storyboard between the two view controllers with an identifier. How can I properly trigger a segue from within my custom view class? How would this normally be done? I tried making a new instance of my view controller so that I could us the .performSegue()
from within the didSelectItemAt
collectionView
function but I don't think this is the proper way to do it at all. example:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController()
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Any suggestions on how to perform the segue from within this function would be greatly appreciated. Below I will supply the key components of my existing code so that you can see what I'm doing. There is a lot more code in both classes but I think this should be enough for the general idea of what I'm trying to accomplish.
view controller:
class UARTModuleViewController: UIViewController, CBPeripheralManagerDelegate {
override func viewDidLoad() {
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
menuBar.translatesAutoresizingMaskIntoConstraints = false
menuBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
menuBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
}
MenuBar.swift:
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
private func setupCollectionView() {
self.addSubview(collectionView)
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "MenuCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UARTModuleViewController() // I want to perform a segue from here
vc.performSegue(withIdentifier: "colorSelection", sender: nil)
}
}
Thanks in advance to anyone who can provide helpful advice and suggestions!
ios swift uicollectionview segue
ios swift uicollectionview segue
asked Nov 25 '18 at 21:02
demogorgondemogorgon
426315
426315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This
let vc = UARTModuleViewController()
Creates another object on the fly that's independent of the presented one and may cause a crash if the vc isn't programmatically created , instead you need a delegate
lazy var menuBar: MenuBar = {
let mb = MenuBar()
mb.delegate = self
return mb
}()
And add this var inside MenuBar
weak var delegate:UARTModuleViewController?
Then use it like this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Should say that a better approach for reusability is to create a protocol
protocol ColManager {
func navigate()
}
With
weak var delegate:ColManager?
And any class that wants to use should conform to the protocol and implement the navigate
method
this makes a lot more sense to me. However, when I now callmb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'
– demogorgon
Nov 25 '18 at 21:12
made itlazy var menuBar: MenuBar
?
– Sh_Khan
Nov 25 '18 at 21:14
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using theweak var delegate:UARTModuleViewController?
and assigning it toself
– demogorgon
Nov 25 '18 at 21:16
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%2f53471948%2fperforming-segue-from-within-a-view%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
This
let vc = UARTModuleViewController()
Creates another object on the fly that's independent of the presented one and may cause a crash if the vc isn't programmatically created , instead you need a delegate
lazy var menuBar: MenuBar = {
let mb = MenuBar()
mb.delegate = self
return mb
}()
And add this var inside MenuBar
weak var delegate:UARTModuleViewController?
Then use it like this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Should say that a better approach for reusability is to create a protocol
protocol ColManager {
func navigate()
}
With
weak var delegate:ColManager?
And any class that wants to use should conform to the protocol and implement the navigate
method
this makes a lot more sense to me. However, when I now callmb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'
– demogorgon
Nov 25 '18 at 21:12
made itlazy var menuBar: MenuBar
?
– Sh_Khan
Nov 25 '18 at 21:14
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using theweak var delegate:UARTModuleViewController?
and assigning it toself
– demogorgon
Nov 25 '18 at 21:16
add a comment |
This
let vc = UARTModuleViewController()
Creates another object on the fly that's independent of the presented one and may cause a crash if the vc isn't programmatically created , instead you need a delegate
lazy var menuBar: MenuBar = {
let mb = MenuBar()
mb.delegate = self
return mb
}()
And add this var inside MenuBar
weak var delegate:UARTModuleViewController?
Then use it like this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Should say that a better approach for reusability is to create a protocol
protocol ColManager {
func navigate()
}
With
weak var delegate:ColManager?
And any class that wants to use should conform to the protocol and implement the navigate
method
this makes a lot more sense to me. However, when I now callmb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'
– demogorgon
Nov 25 '18 at 21:12
made itlazy var menuBar: MenuBar
?
– Sh_Khan
Nov 25 '18 at 21:14
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using theweak var delegate:UARTModuleViewController?
and assigning it toself
– demogorgon
Nov 25 '18 at 21:16
add a comment |
This
let vc = UARTModuleViewController()
Creates another object on the fly that's independent of the presented one and may cause a crash if the vc isn't programmatically created , instead you need a delegate
lazy var menuBar: MenuBar = {
let mb = MenuBar()
mb.delegate = self
return mb
}()
And add this var inside MenuBar
weak var delegate:UARTModuleViewController?
Then use it like this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Should say that a better approach for reusability is to create a protocol
protocol ColManager {
func navigate()
}
With
weak var delegate:ColManager?
And any class that wants to use should conform to the protocol and implement the navigate
method
This
let vc = UARTModuleViewController()
Creates another object on the fly that's independent of the presented one and may cause a crash if the vc isn't programmatically created , instead you need a delegate
lazy var menuBar: MenuBar = {
let mb = MenuBar()
mb.delegate = self
return mb
}()
And add this var inside MenuBar
weak var delegate:UARTModuleViewController?
Then use it like this
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.performSegue(withIdentifier: "colorSelection", sender: nil)
}
Should say that a better approach for reusability is to create a protocol
protocol ColManager {
func navigate()
}
With
weak var delegate:ColManager?
And any class that wants to use should conform to the protocol and implement the navigate
method
edited Nov 25 '18 at 21:42
answered Nov 25 '18 at 21:04
Sh_KhanSh_Khan
44.6k51432
44.6k51432
this makes a lot more sense to me. However, when I now callmb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'
– demogorgon
Nov 25 '18 at 21:12
made itlazy var menuBar: MenuBar
?
– Sh_Khan
Nov 25 '18 at 21:14
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using theweak var delegate:UARTModuleViewController?
and assigning it toself
– demogorgon
Nov 25 '18 at 21:16
add a comment |
this makes a lot more sense to me. However, when I now callmb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'
– demogorgon
Nov 25 '18 at 21:12
made itlazy var menuBar: MenuBar
?
– Sh_Khan
Nov 25 '18 at 21:14
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using theweak var delegate:UARTModuleViewController?
and assigning it toself
– demogorgon
Nov 25 '18 at 21:16
this makes a lot more sense to me. However, when I now call
mb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'– demogorgon
Nov 25 '18 at 21:12
this makes a lot more sense to me. However, when I now call
mb.delegate = self
after making the suggested changes, I receive this error on the same line Cannot assign value of type '(UARTModuleViewController) -> () -> (UARTModuleViewController)' to type 'UARTModuleViewController?'– demogorgon
Nov 25 '18 at 21:12
made it
lazy var menuBar: MenuBar
?– Sh_Khan
Nov 25 '18 at 21:14
made it
lazy var menuBar: MenuBar
?– Sh_Khan
Nov 25 '18 at 21:14
1
1
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using the
weak var delegate:UARTModuleViewController?
and assigning it to self
– demogorgon
Nov 25 '18 at 21:16
ahh i missed that edit. this worked perfectly! thank you for the help! it makes a lot more sense using the
weak var delegate:UARTModuleViewController?
and assigning it to self
– demogorgon
Nov 25 '18 at 21:16
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%2f53471948%2fperforming-segue-from-within-a-view%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