I'm getting black flickering screen while dismissing PopOverViewController.
Code in ViewController.swift
if segue.identifier == "addComment"
{
let controller = segue.destinationViewController as! ProfileCommentPopOver
controller.popoverPresentationController?.delegate = self
controller.preferredContentSize = CGSizeMake(250, 150)
controller.fetchedProfileID = sendToUserID
}
PopOverViewController.swift
#IBAction func addCommentAction(sender: UIButton)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
try
#IBAction func addCommentAction(sender: UIButton)
{
self.dismiss(animated: true, completion:{})
}
Related
I have my main screen with only one button on it "Show next screen". When the second screen(VC) pops up it has 2 buttons (go back and toSelect button).
My goal is to when I show my second screen and select a button on it then go back to first. The button on my second screen will stay selected. How can I do that?
So basically I need to save my actions on the second screen so if I go back to it it will show everything I did.
What is the best way to do it?
Storyboard
The easiest way to achieve this using Delegate and protocol.
you should listen and save the changes of SecondViewController at FirstViewController using delegate methods.
And when you are presenting the secondViewController you will share the saved changes to secondViewController so that button can be selected on behalf of that information
Code -
class FirstViewController: UIViewController {
//secondViewController States
private var isButtonSelected = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func gotoSecondVCAction(_ sender: Any) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
guard let secondVC = storyBoard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
secondVC.isButtonSelected = isButtonSelected
secondVC.delegate = self
self.present(secondVC, animated: true, completion: nil)
}
}
extension FirstViewController: ButtonSelectionProtocol {
func button(isSelected: Bool) {
isButtonSelected = isSelected
}
}
and for secondViewController
protocol ButtonSelectionProtocol {
func button(isSelected:Bool)
}
class SecondViewController: UIViewController {
var isButtonSelected : Bool = false
var delegate:ButtonSelectionProtocol?
#IBOutlet weak var selectButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if isButtonSelected {
selectButton.tintColor = .red
selectButton.setTitle("Selected", for: .normal)
}else{
selectButton.tintColor = .green
selectButton.setTitle("Select Me", for: .normal)
}
}
#IBAction func gobackAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
#IBAction func selectAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
isButtonSelected.toggle()
delegate?.button(isSelected: isButtonSelected)
}
}
I made it using a storyboard
I made another view controller for the custom tab bar and created a UIView called innerContents.
And I made another view controller for the setting page in it and put another view controller in innerContents with addSubview.
CustomTabBar with innerContents UIView
thats innerTabBar main CustomTabBar View Controller page
other ViewController
i will put thats ViewController in innerContents. so i writed addSubView. they are showing but UIButtons are not working. only work ScrollView.
How can I fix it?
code
I confirmed that all outlets are connected normally.
But the UIButton inside the UIView inside the UIScrollView inside the UiView of another view controller inside the UiView doesn't work.
mainCustomTabBar Page viewController > innerContents > otherViewController(addSubView) > UIView > UI ScrollView > UIView > UIView > UIButton(it's not working. i want fix that)
if currentPage != 5 {
currentPage = 5
print("탭바 클릭 5")
guard let AppSet = self.storyboard?.instantiateViewController(identifier: "AppSetViewController") as? AppSetViewController else {return}
AppSet.view.frame = ContentsView.bounds
AppSet.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
AppSet.view.tag = 500
removeSubview(tag: nowTag)
nowTag = 500
ContentsView.addSubview(AppSet.view)
let root = UIApplication.shared.windows.first?.rootViewController
root?.addChild(AppSet)
AppSet.didMove(toParent: root!)
page5Mode()
return
}
}
//
// AppSetViewController.swift
// rael
//
// Created by HongDaehyeon on 2021/07/08.
// 앱 설정 페이지를 위한 페이지 입니다.
import UIKit
class AppSetViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet var ContentView: UIView!{
didSet {
self.ContentView.isUserInteractionEnabled = true
}
}
#IBOutlet var Scrollview: UIScrollView!{
didSet{
self.Scrollview.isExclusiveTouch = true
}
}
//지갑정보
#IBAction func walletInfo(_ sender: Any) {
print("지갑정보")
}
//알림설정
#IBAction func alertSet(_ sender: Any) {
print("알림설정")
}
//인증방식설정
#IBAction func authSet(_ sender: Any) {
print("인증방식설정")
}
//공지사항
#IBAction func noticeNav(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(identifier: "SettingsNoticeViewController")
let navController = UINavigationController(rootViewController: vc!)
navController.modalPresentationStyle = .fullScreen
navController.modalTransitionStyle = .crossDissolve
navController.setNavigationBarHidden(true, animated: false)
self.present(navController, animated: true, completion: nil)
}
//FAQ
#IBAction func faqNav(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(identifier: "FaqViewController")
let navController = UINavigationController(rootViewController: vc!)
navController.modalPresentationStyle = .fullScreen
navController.modalTransitionStyle = .crossDissolve
navController.setNavigationBarHidden(true, animated: false)
self.present(navController, animated: true, completion: nil)
}
//고객상담
#IBAction func serviceCenter(_ sender: Any) {
print("고객상담")
}
//약관 및 정책
#IBAction func termsNav(_ sender: Any) {
print("약관 및 정책")
}
//지갑 삭제
#IBAction func removeWallet(_ sender: Any) {
print("지갑삭제")
}
//전화걸기
#IBAction func callService(_ sender: Any) {
print("전화걸기")
let urlString = "tel://1588-8282"
let numberURL = NSURL(string: urlString)
UIApplication.shared.open(numberURL! as URL, options: [:], completionHandler: nil)
}
//앱정보 로딩
#IBOutlet var appVer: UILabel!{
didSet{
self.appVer.text = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
}
}
}```
I have created .xib files and connected them with appropriate view controllers. Unfortunately, it doesn't go well.
I have checked and tried so many examples, but not working for newest version. Using Xcode 11.3 and iOS 13.
here is the code I have tried.
TopView.swift
#IBAction func btnConnectTapped(_ sender: Any) {
print("tapped")
var listVC: PopupView // This one is UIView and popped the window
listVC = Bundle.main.loadNibNamed("PopupView", owner: self, options: nil)?.first as! PopupView
self.view.addSubview(listVC)
// Tried for the UIViewController and not worked
/*let VC = ScanViewController(nibName: "ScanViewController", bundle: nil)
//self.present(VC, animated: true, completion: nil)
self.navigationController?.pushViewController(VC, animated: true)*/
}
Popup.swift // dismiss the popped view
#IBAction func btnCancelTapped(_ sender: Any) {
DispatchQueue.main.async {
self.removeFromSuperview()
}
}
both ScanViewController and Popup view have same purpose.
Can someone help me
pod Url: https://github.com/Orderella/PopupDialog
eg to make Popup:
let ratingVC = TimeSelection(nibName: "TimeSelection", bundle: nil)
ratingVC.view.backgroundColor = .white
ratingVC.delegate = self
let popup = PopupDialog(viewController: ratingVC,
buttonAlignment: .horizontal,
transitionStyle: .bounceDown,
tapGestureDismissal: true,
panGestureDismissal: true)
popup.popupContainerView.backgroundColor = .clear
present(popup, animated: true, completion: nil)
TopView.swift
#IBAction func btnTap(_ sender: UIButton)
{
if PopupScreensharedInstance != nil
{
PopupScreensharedInstance.makeInstanceNil()
}
view.addSubview(popView.SharedInstance())
}
popView.swift
var PopupScreensharedInstance : popView! = nil
class popView: UIView {
class func SharedInstance() ->popView
{
if(PopupScreensharedInstance == nil)
{
PopupScreensharedInstance = (Bundle.main.loadNibNamed("popView", owner: self, options: nil)![0] as! popView)
PopupScreensharedInstance.frame = UIScreen.main.bounds
}
return PopupScreensharedInstance
}
func makeInstanceNil()
{
PopupScreensharedInstance = nil
self.removeFromSuperview()
}
#IBAction func cancle(_ sender: UIButton)
{
makeInstanceNil()
}
}
I spent hours to make this work but I am trying to show an array in a view controller based on the button that is pressed in the previous view. So I want to use the same view controller but with different data.
In the view controller:
#IBAction func firstDeck(_ sender: UIButton) {
var dataSource : CardsDataModel? {
didSet {
label.text = cardsDeck.FirstDate()
}
}
}
#IBAction func secondDeck(_ sender: UIButton) {
var dataSource : CardsDataModel? {
didSet {
label.text = cardsDeck.PizzaFriends()
}
}
}
So there are two cardDecks: PizzaFriends and FirstDates. I tried using IBAction from the buttons to then show the relevant cardsDeck but without success. The cards are empty.
Declare an Array of String type variable in NextViewController
var someData = [String]()
In viewDidLoad() method of NextViewController add this
print("Some Data: \(someData)")
In the PreviousViewController in the IBAction methods present/push the NextViewController with assign someText variable according to which button has been pressed
#IBAction func firstDeck(_ sender: UIButton) {
let vc = NextViewController()
vc.someData = cardsDeck.FirstDate()
// If you want to present NextViewController
present(vc, animated: true, completion: nil)
// If you want to push NextViewController to navigation stack
//navigationController?.pushViewController(vc, animated: true)
}
#IBAction func secondDeck(_ sender: UIButton) {
let vc = NextViewController()
vc.someData = cardsDeck.PizzaFriends()
// If you want to present NextViewController
present(vc, animated: true, completion: nil)
// If you want to push NextViewController to navigation stack
//navigationController?.pushViewController(vc, animated: true)
}
Here is a sample NextViewController
class NextViewController: UIViewController {
var someData = [String]()
var cardView = SwipeCardView(frame: CGRect(x: 10, y: 150, width: 300, height: 600))
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.view.addSubview(cardView)
cardView.label.text = "\(someData)"
}
}
I try to present VCs but buttons in popover menu, but I have hierarchy warnings like this:
Warning: Attempt to present "UIViewController: 0x14def7500" on "MyProject.MainViewController: 0x14f976400" whose view is not in the window hierarchy!
I have MainViewController and PopupMenu VCs classes:
Swift 4.0
class MainViewController: UIViewController, UIPopoverPresentationControllerDelegate {
//... here is my VC code
// showing Popup Menu VC
#IBAction func showPopupMenu(sender: UIButton) {
menuVC = PopupMenu()
menuVC?.modalPresentationStyle = .popover
menuVC?.preferredContentSize = CGSize(width: 150, height: 250)
if let pvc = menuVC?.popoverPresentationController {
pvc.permittedArrowDirections = .up
pvc.delegate = self
pvc.sourceView = sender
pvc.sourceRect = sender.bounds
}
self.present(menuVC!, animated: true, completion: nil)
}
// showing VC from popupMenu VC
#IBAction func showVCFromPopup(from target: PopupMenu, vc: UIViewController) {
target.dismiss(animated: false, completion: nil) // dismiss popup
if target.isBeingDismissed { // check is popup dismissed
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: true, completion: nil)
}
}
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
}// end of class
class PopupMenu: UIViewController {
var button = UIButton()
// here is init's
override func viewDidLoad() {
//... some other code
button.addTarget(self, action: #selector(vcOpen(sender:)), for: .touchUpInside)
}
#IBAction func vcOpen(sender: UIButton) {
if sender == button {
let vc = UIViewController()
if parent != nil { print("PARENT")} // Never will work, no ideas why, so MainVC isn't a parent of PopupMenu
if let mainVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as? MainViewController {
print("# ACTION: Opening VC")
mainVC.showVCFromPopup(target: self, as: vc!) // opening VC
}
}
}
}
But I have warning.
Maybe anyone will find mistakes in my code or have any ideas how to do this.
Thanks for all answers!
I edited you code to pass a reference of the mainVC to the PopupMenu:
class MainViewController: UIViewController, UIPopoverPresentationControllerDelegate {
// showing Popup Menu VC
#IBAction func showPopupMenu(sender: UIButton) {
menuVC = PopupMenu()
menuVC?.modalPresentationStyle = .popover
menuVC?.preferredContentSize = CGSize(width: 150, height: 250)
menuVC?.MainVC = self <--- here
if let pvc = menuVC?.popoverPresentationController {
pvc.permittedArrowDirections = .up
pvc.delegate = self
pvc.sourceView = sender
pvc.sourceRect = sender.bounds
}
self.present(menuVC!, animated: true, completion: nil)
}
}
class PopupMenu: UIViewController {
var mainVC: UIViewController <-- here
#IBAction func vcOpen(sender: UIButton) {
if sender == button {
mainVC.showVCFromPopup(target: self, as: vc!) <-- here
}
}
}