'Expected declaration' error in init function (swift) - ios

Can someone help me figure out why I can't fix this error in Swift using Xcode6? I'm trying to initialize all my buttons so they aren't nil and it's telling me I'm not declaring something when I thought I was. Any suggestions? The required init method line is giving me the error.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var displayLabel: UILabel!
#IBOutlet weak var buttonOne: UIButton!
#IBOutlet weak var buttonTwo: UIButton!
#IBOutlet weak var buttonThree: UIButton!
#IBOutlet weak var buttonClear: UIButton!
#IBOutlet weak var buttonFour: UIButton!
#IBOutlet weak var buttonFive: UIButton!
#IBOutlet weak var buttonSix: UIButton!
#IBOutlet weak var buttonX: UIButton!
#IBOutlet weak var buttonSeven: UIButton!
#IBOutlet weak var buttonEight: UIButton!
#IBOutlet weak var buttonNine: UIButton!
#IBOutlet weak var buttonDiv: UIButton!
#IBOutlet weak var buttonPlus: UIButton!
#IBOutlet weak var buttonZero: UIButton!
#IBOutlet weak var buttonMin: UIButton!
#IBOutlet weak var buttonEq: UIButton!
#IBOutlet weak var buttonSet: UIButton!
var userIsTyping: Bool = false
required init(coder aDecoder: NSCoder) {
displayLabel = UILabel()
buttonOne = UIButton()
buttonTwo = UIButton()
buttonThree = UIButton()
buttonFour = UIButton()
buttonFive = UIButton()
buttonSix = UIButton()
buttonSeven = UIButton()
buttonEight = UIButton()
buttonNine = UIButton()
buttonZero = UIButton()
buttonClear = UIButton()
buttonX = UIButton()
buttonPlus = UIButton()
buttonDiv = UIButton()
buttonMin = UIButton()
buttonEq = UIButton()
buttonSet = UIButton()
super.init(coder: aDecoder)
}

From Apple documentation:
An outlet is a property of an object that references another object. The reference is archived through Interface Builder. The connections between the containing object and its outlets are reestablished every time the containing object is unarchived from its nib file. The containing object holds an outlet declared as a property with the type qualifier of IBOutlet and a weak option.
So, you should not instantiate a #IBOutlet object, since they are meant to be connected to a UI object on storyboard or .nib file.
You've said that you are doing this to avoid nil pointer. When does that happen?

Related

How add gesture into the tableviewcell?

everyone!
I am trying to add gesture to a UIImageView into the TableViewCell.
When click on the ImageView - the color changes.
class StocksCellView: UITableViewCell {
#IBOutlet weak var logoImageView: UIImageView!
#IBOutlet weak var tickerLabel: UILabel!
#IBOutlet weak var favouriteImageView: UIImageView!{ didSet {
let panGesture = UITapGestureRecognizer(target: self,
action: #selector(gestureAction))
favouriteImageView.addGestureRecognizer(panGesture)
}
}
#IBOutlet weak var companyNameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var deltaLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
#objc func gestureAction() {
favouriteImageView.tintColor = UIColor.yellow
}
}
But nothing works.
If i change favouriteImageView.addGestureRecognizer(panGesture)
on addGestureRecognizer(panGesture)
Then, by clicking on the cell, the color will change.
But I want by clicking on the picture.
enter image description here
First of all you must check and change like below:
favouriteImageView.isUserInteractionEnabled = true

Hierarchy Problem with presenting Viewcontrollers, can someone help me?

[![enter image description here][2]][2]i want to programm an App with two ViewControllers
The first one performs a segue to the second VC.
The Second VC has a Scrollview with different Views.
I did everything with Autolayout not with code
When i run the Application i get always the error "whose view is not in the window hierarchy!" and can't scroll to the bottom. I tried to find some solutions but nothing helped.
Hope someone can help me with the problem.
my first VC runs a normal performSegue("identifier",sender )
my code for the second VC is
import UIKit
class FragenViewController: UIViewController {
var fragen = [Question]()
var highscore = 0
var questionNumber = 0
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var answerButton1: UIButton!
#IBOutlet weak var answerButton2: UIButton!
#IBOutlet weak var answerButton3: UIButton!
#IBOutlet weak var answerButton4: UIButton!
#IBOutlet weak var forwardButton: UIButton!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet weak var currentQuestionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func answerButton_Tapped(_ sender: UIButton) {
}
}
the rest is with Main Storyboard Drag and drop
The Code from the first VC is
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var highscoreLabel: UILabel!
#IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Setup Layout
startButton.backgroundColor = UIColor.systemFill
startButton.layer.cornerRadius = 20
startButton.titleLabel?.font = UIFont(name: "Arial", size: 20)
startButton.layer.borderWidth = 2
startButton.tintColor = UIColor.systemBlue
highscoreLabel.font = UIFont(name: "Arial", size: 18)
}
#IBAction func start_Button(_ sender: Any) {
performSegue(withIdentifier: "startGame", sender: sender)
}
}

Why Does My App Crash due to Memory Issue?

I am dealing with an app created by some other developer. It's a complete app and has a lot of viewControllers , variables and outlets.
I keep getting the a crash after I load too many images from a server ( 200 for example ). I only get this message in the "print area" : "App terminated due to memory issue".
I use the library "SDWebImage" for loading the images. And I tried to find a memory leak using Instruments Allocations, and Leaks. I also used Memory Graph Debugger and non of them show leaks in my app.
Yet when I pop the View Controller ( DetailVC ) , it never fires the deinit method where I have put a message to print when this happens.
I have searched a lot to no vail. I have looked at these on Stackoverflow :
App Extension "Terminated due to memory issue"
App terminated due to memory issue
Through out my search I repeatedly see that response that the View Controller must be referenced by another view controller and this View Controller (DetailVC) strongly referencing the other.
I couldn't find that to be the case, although the file for the view controller is too large and I may have missed things.
It's difficult to go through the app and look for strong and weak references as the file is really huge.
is there a simple way ( or difficult for that matter ) to find the culprit and solve my problem.
Thanks
the code is huge (95000 characters) and contains sensitive information thus is not appropriate to post here. although I can post parts of it should you ask for it.
here is the code for DetailVC viewDidLoad :
import UIKit
import FTIndicator
import Cosmos
import Firebase
import MapKit
import YouTubePlayer
//MARK:- Gallery Collection Cell
class GalleryCollectionCell:UICollectionViewCell
{
#IBOutlet weak var imgViewShop: UIImageView!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
//MARK:- Service Collection Cell
class ServiceCollectionCell:UICollectionViewCell
{
#IBOutlet weak var imgViewService: UIImageView!
#IBOutlet weak var lblService: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
#IBAction func btnBackAction(_ sender: Any)
{
self.navigationController?.popViewController(animated: true)
SDImageCache.shared().clearMemory()
SDImageCache.shared().clearDisk()
}
//MARK:- Detail Main Class
class DetailVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UITableViewDataSource, UITableViewDelegate,RateFinalDelegate, PhotoDicDelegate
{
//MARK:- Outlets
#IBOutlet weak var tblViewRate: UITableView!
#IBOutlet weak var tblViewService: UITableView!
#IBOutlet weak var lblServices: UILabel!
#IBOutlet weak var collViewGallery: UICollectionView!
//#IBOutlet weak var collViewService: UICollectionView!
#IBOutlet weak var constTableViewHeight: NSLayoutConstraint!
#IBOutlet weak var constTlbViewServiceHeight: NSLayoutConstraint!
#IBOutlet weak var imgViewShop: UIImageView!
#IBOutlet weak var lblShopName: UILabel!
//#IBOutlet weak var lblShopNameDetail: UILabel!
#IBOutlet weak var btnFavourites: UIButton!
#IBOutlet weak var viewStar: CosmosView!
#IBOutlet weak var lblReviewCount: UILabel!
#IBOutlet weak var btnShopStatus: UIButton!
#IBOutlet weak var lblShopOnline: UILabel!
// #IBOutlet weak var lblDetailText: UILabel!
#IBOutlet weak var btnShopRate: UIButton!
#IBOutlet weak var lblShopAddress: UILabel!
#IBOutlet weak var lblShopWebsite: UILabel!
#IBOutlet weak var lblShopView: UILabel!
#IBOutlet weak var lblShopOpenStatus: UILabel!
//#IBOutlet weak var lblPhone1: UILabel!
#IBOutlet weak var lblShopDetail: UILabel!
// #IBOutlet weak var btnFacebook: UIButton!
// #IBOutlet weak var btnSnapchat: UIButton!
// #IBOutlet weak var btnInstagram: UIButton!
// #IBOutlet weak var btnTwitter: UIButton!
// #IBOutlet weak var btnYoutube: UIButton!
// #IBOutlet weak var btnVivo: UIButton!
// #IBOutlet weak var btnGoogle: UIButton!
#IBOutlet weak var lblSat: UILabel!
#IBOutlet weak var lblSun: UILabel!
#IBOutlet weak var lblMon: UILabel!
#IBOutlet weak var lblTues: UILabel!
#IBOutlet weak var lblWed: UILabel!
#IBOutlet weak var lblThru: UILabel!
#IBOutlet weak var lblFri: UILabel!
#IBOutlet weak var lblSatText: UILabel!
#IBOutlet weak var lblSunText: UILabel!
#IBOutlet weak var lblMonText: UILabel!
#IBOutlet weak var lblTuesText: UILabel!
#IBOutlet weak var lblWedText: UILabel!
#IBOutlet weak var lblThruText: UILabel!
#IBOutlet weak var lblFriText: UILabel!
#IBOutlet weak var lblworkingHour: UILabel!
#IBOutlet weak var lblGallery: UILabel!
#IBOutlet weak var lblReviews: UILabel!
#IBOutlet weak var btnSeeMore: UIButton!
#IBOutlet weak var btnBack: UIButton!
#IBOutlet weak var viewMain: UIView!
#IBOutlet weak var viewSuper: UIView!
#IBOutlet weak var viewService: UIView!
#IBOutlet weak var btnWebsite: UIButton!
#IBOutlet weak var btnPhone1: UIButton!
#IBOutlet weak var btnPhone2: UIButton!
#IBOutlet weak var btnPhone3: UIButton!
#IBOutlet weak var constPhone1Height: NSLayoutConstraint!
#IBOutlet weak var constPhone2Height: NSLayoutConstraint!
#IBOutlet weak var constPhone3Height: NSLayoutConstraint!
#IBOutlet weak var constPhoneViewHeight: NSLayoutConstraint!
#IBOutlet weak var constViewScrollHeight: NSLayoutConstraint!
#IBOutlet weak var scrollSocial: UIScrollView!
// #IBOutlet weak var btnSocialLink: UIButton!
#IBOutlet weak var btnGallery: UIButton!
#IBOutlet weak var constViewServiceHeight: NSLayoutConstraint!
//For Photo Class
#IBOutlet weak var tblViewHeader: UITableView!
#IBOutlet weak var switchGallery: UISwitch!
#IBOutlet weak var constTableViewGalleryHeight: NSLayoutConstraint!
var albumListArray = [AlbumListData]()
//For Video Class
#IBOutlet weak var collectionViewVideos: UICollectionView!
#IBOutlet weak var constCollViewVideoHeight: NSLayoutConstraint!
var videoDataArray = [VideoListData]()
var switchStatus = Bool()
//MARK:- Variables
let globalConstants = GlobalConstants()
var UserData = UserDataValue()
var reviewDataArray = [Review]()
var galleryDataArray = [Gallery]()
var serviceDataArray = [Service]()
var ShopId = String()
var favStatus = String()
var ShopStatus = String()
var Latitude = String()
var Longitude = String()
var strFacebook = String()
var strInstagram = String()
var NotifyId = String()
var strTwitter = String()
var strSnapchat = String()
var strYoutube = String()
var strGoogle = String()
var strVimeo = String()
var chatStatus = Bool()
var isfirstTime = Bool()
var receiverId = ""
var receiverImage = ""
var receiverName = ""
var strWebsite = ""
var shopOwnerId = ""
var ShopUnqueId = ""
var HideChatStatus = ""
var IsChatCreateScreen = ""
var strPhone1Number = String()
var strPhone2Number = String()
var strPhone3Number = String()
var RateText = Bool()
var ShopDeliveryServiceItself = Bool()
var CitySelectedId = String()
var OneToOneChatUserData : NSDictionary = [:]
var ShopString = "1159,1160,1162,1166,1167,1176,1178,1179,1180,1182,1184,1185,1186,1187,1188,1189,1190,1191,1192,1193,1194,1199,1202,1203,1206,1208,1209,1210,1214,1216,1217,1218,1224,1225,1227,1230,1232,1233,1234,1235,1236,1238,1239,1240,1242,1243,1244,1245,1246,1247,1248,1250,1252,1253,1255,1258,1259,1263,1264,1265,1266,1269,1270,1272,1275,1276,1277,1278,1279,1280,1281,1282,1283,1285,1298,1299,1302";
//MARK:- View Life Cycle
override func viewDidLoad()
{
super.viewDidLoad()
// collViewGallery.dataSource = self
// collViewGallery.delegate = self
// collViewService.dataSource = self
// collViewService.delegate = self
btnShopStatus.setTitle(" CHAT".localiz(), for: .normal)
btnSeeMore.setTitle("See More".localiz(), for: .normal)
btnShopRate.setTitle("RATE".localiz(), for: .normal)
btnGallery.setTitle("Gallery".localiz(), for: .normal)
lblworkingHour.text = "Working Hours:".localiz()
lblServices.text = "Services".localiz()
lblGallery.text = "Gallery".localiz()
lblReviews.text = "REVIEWS".localiz()
//lblDetailText.text = "DETAILS".localiz()
lblSatText.text = "Saturday".localiz()
lblSunText.text = "Sunday".localiz()
lblMonText.text = "Monday".localiz()
lblTuesText.text = "Tuesday".localiz()
lblWedText.text = "Wednesday".localiz()
lblThruText.text = "Thursday".localiz()
lblFriText.text = "Friday".localiz()
viewStar.settings.fillMode = .precise
self.navigationItem.title = globalConstants.detailText
tblViewRate.register(UINib(nibName: "RateTableVCell", bundle: nil), forCellReuseIdentifier: "RateTableVCell")
tblViewService.register(UINib(nibName: "ServiceTableCell", bundle: nil), forCellReuseIdentifier: "ServiceTableCell")
tblViewHeader.register(UINib(nibName: "SubCatHeaderTVC", bundle: nil), forCellReuseIdentifier: "SubCatHeaderTVC")
tblViewHeader.dataSource = self
tblViewHeader.delegate = self
tblViewHeader.estimatedRowHeight = 120
tblViewHeader.rowHeight = UITableViewAutomaticDimension
tblViewService.dataSource = self
tblViewService.delegate = self
tblViewService.estimatedRowHeight = 50
tblViewService.rowHeight = UITableViewAutomaticDimension
self.constTlbViewServiceHeight.constant = 20
collectionViewVideos.register(UINib(nibName: "VideoCollViewCell", bundle: nil), forCellWithReuseIdentifier: "VideoCollViewCell")
collectionViewVideos.dataSource = self
collectionViewVideos.delegate = self
collectionViewVideos.reloadData()
switchStatus = false
switchGallery.setOn(false, animated: true)
//self.constViewServiceHeight.constant = 20
tblViewRate.dataSource = self
tblViewRate.delegate = self
tblViewRate.estimatedRowHeight = 100
tblViewRate.rowHeight = UITableViewAutomaticDimension
//self.constTableViewGalleryHeight.constant = 20
self.constTableViewHeight.constant = 20
// self.tblViewHeader.isHidden = false
// self.collectionViewVideos.isHidden = true
// self.constCollectionViewHeight.constant = 30
ShopStatus = ""
self.addBackButton()
UserDefaults.standard.set(ShopId, forKey: "ShopValueId")
UserDefaults.standard.synchronize()
isfirstTime = true
viewSuper.backgroundColor = UIColor.lightGray
viewMain.isHidden = true
if LanguageManger.shared.currentLanguage == .en
{
lblServices.textAlignment = .left
btnBack.setImage(UIImage(named:"back"), for: .normal)
}
else
{
lblServices.textAlignment = .right
btnBack.setImage(UIImage(named:"ReverseBack"), for: .normal)
}
// if KAppDelegate.isUserLoggedIn()
// {
// let userDic = UserDefaults.standard.value(forKey: "UserData") as! [String:Any]
// self.UserData = UserDataValue.init(fromDictionary: userDic)
// UserId = self.UserData.id!
// UserName = self.UserData.name!
// }
if KAppDelegate.isUserLoggedIn()
{
let userDic = UserDefaults.standard.value(forKey: "UserData") as! [String:Any]
self.UserData = UserDataValue.init(fromDictionary: userDic)
CitySelectedId = self.UserData.city!
}
else
{
if UserDefaults.standard.value(forKey: "CitySelectedId") != nil
{
CitySelectedId = UserDefaults.standard.value(forKey: "CitySelectedId") as! String
}
else
{
CitySelectedId = "1"
}
}
ShopDetailAPIMethod()
}
override func viewWillAppear(_ animated: Bool)
{
self.navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool)
{
self.IsChatCreateScreen = ""
self.navigationController?.navigationBar.isHidden = false
}
// override var preferredStatusBarStyle: UIStatusBarStyle
// {
// return .default
// }
//MARK:- Photo Dic Delegate Method
//MARK:- CreateNewChat Method
}
and this is how instantiate the DetailVC :
#objc func methodOfNotification(notification: Notification)
{
if UserDefaults.standard.value(forKey: "ShopValueId") != nil
{
let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
detailVC.ShopId = UserDefaults.standard.value(forKey: "ShopValueId") as! String
self.navigationController?.pushViewController(detailVC, animated: true)
}
}
Here is the scenario:
I am within subCategoryVC I click on a collectionView cell, this instantiates the DetailVC. within the DetailVC I click on the seeAll button to load the images in the collection view ( this is done using SDWebImage using this method :
l
let imageStringURL = ShopDetailData.coverImage!
imgViewShop.sd_setShowActivityIndicatorView(true)
imgViewShop.sd_setIndicatorStyle(.gray)
imgViewShop.sd_setImage(with: NSURL(string:imageStringURL)! as URL, placeholderImage:UIImage(named:"noimage") , options: .refreshCached, completed: nil)
when I click on the btnBack(back button) and I pop the view controller using navigationContrller.popViewController() this is where the deinit message should be printed in the "print area" but this never happens.
furthermore when I open 2 or 3 detailVC's and push the seeAll button the app crashes and "print area" shows
"app terminated due to memory issue".
I just used Instruments to check for memory leak. there is one leak as it turns out and with these details :
leaked object = _swiftStringStorage<UInt16>
responsible library = libswiftCore.dylib
responsible frame = swift_slowAlloc
The Apps allows limited memory. I guess after 800mb , app is shut down with memory issue. This is not a true way that is downloaded 200 images for one controller but you can use with tableview with dequereuaseble and sdwebimage or kingfisher. Also you can use thumbnail images because you dont need to show 1 mb image in imageview , this is not unnecessary.
You probably have a retain-cycle. This means that some of the subviews of your ViewController has a strong reference to the ViewController itself or to another object that has the strong reference to the ViewController. When this happens, even if you pop the ViewController it won't be destroyed until the subview is destroyed, which will never be destroyed while the ViewController exists.
If this is your case, the solution would be searching for this circular reference in your subviews and put a "weak" in the variable that references the ViewController. Searching for places where you assign "self" to a variable or where you use "self" inside an enclosure could help.

cannot use instance member within property initializer DropDown library [duplicate]

This question already has answers here:
How to initialize properties that depend on each other
(4 answers)
Closed 5 years ago.
i have implemented assistolab dropdown in my project.but in some viewcontroller it could not work properly.it shows,
cannot use instance member within property initializer
my code snippet is given below
class MobileTopUpVC: UIViewController,UITextFieldDelegate {
let dropDown = DropDown() //error is here
#IBOutlet weak var mobileTopUpImag: UIImageView!
#IBOutlet weak var amountTxt: UITextField!
#IBOutlet weak var mobileNumberTxt: UITextField!
#IBOutlet weak var operatorTxt: UITextField!
#IBOutlet weak var countryTxt: UITextField!
#IBOutlet weak var DropDown: UIView!
#IBOutlet weak var backGroundView: UIView!
var textArray = [UITextField]()
var countryArray:[String] = colors.countryArray
var operatorArray:[String] = colors.opArray
#IBOutlet weak var proceedButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
backGroundView.layer.cornerRadius = backGroundView.frame.width/2
backGroundView.clipsToBounds = true
//countryArray = colors.countryArray
backGroundView.layer.borderWidth = 3
backGroundView.layer.borderColor = colors.second.cgColor
textArray = [countryTxt,operatorTxt,amountTxt,mobileNumberTxt]
let view1:UIView! = UIView()
view1.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
countryTxt.inputView = view1
operatorTxt.inputView = view1
// countryTxt.layer.borderWidth = 1
// countryTxt.layer.borderColor = UIColor.green.cgColor
// countryTxt.borderStyle = UITextBorderStyle.bezel
borderStyle()
proceedButton.Cradius(size: 15.0)
mobileTopUpImag.image = mobileTopUpImag.image?.withRenderingMode(.alwaysTemplate)
mobileTopUpImag.tintColor = colors.second
countryDropDown()
}
the error is troubling me in this line
let dropDown = DropDown()
please help me to resolve this
Change your code to the following so your DropDown is initialised in the viewDidLoad
and also change your View name From "DropDown" to something else :D
class MobileTopUpVC: UIViewController,UITextFieldDelegate {
let dropDown: DropDown? //Changed**
#IBOutlet weak var mobileTopUpImag: UIImageView!
#IBOutlet weak var amountTxt: UITextField!
#IBOutlet weak var mobileNumberTxt: UITextField!
#IBOutlet weak var operatorTxt: UITextField!
#IBOutlet weak var countryTxt: UITextField!
#IBOutlet weak var dropDownView: UIView! // Change your VIEW NAME!!!
#IBOutlet weak var backGroundView: UIView!
var textArray = [UITextField]()
var countryArray:[String] = colors.countryArray
var operatorArray:[String] = colors.opArray
#IBOutlet weak var proceedButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
dropDown = DropDown()
backGroundView.layer.cornerRadius = backGroundView.frame.width/2
backGroundView.clipsToBounds = true
//countryArray = colors.countryArray
backGroundView.layer.borderWidth = 3
backGroundView.layer.borderColor = colors.second.cgColor
textArray = [countryTxt,operatorTxt,amountTxt,mobileNumberTxt]
let view1:UIView! = UIView()
view1.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
countryTxt.inputView = view1
operatorTxt.inputView = view1
// countryTxt.layer.borderWidth = 1
// countryTxt.layer.borderColor = UIColor.green.cgColor
// countryTxt.borderStyle = UITextBorderStyle.bezel
borderStyle()
proceedButton.Cradius(size: 15.0)
mobileTopUpImag.image = mobileTopUpImag.image?.withRenderingMode(.alwaysTemplate)
mobileTopUpImag.tintColor = colors.second
countryDropDown()
}
You are using DropDown class as property name. Check below line in your code-
#IBOutlet weak var DropDown: UIView!
It should be something like this-
#IBOutlet weak var dropDownView: UIView!

Give buttons same behavior (twins)

I would like my UIButtons to interact with each other - (With the corresponding button)
First set of buttons;
#IBOutlet var P1button1: UIButton!
#IBOutlet var P1button2: UIButton!
#IBOutlet var P1button3: UIButton!
#IBOutlet var P1button4: UIButton!
#IBOutlet var P1button5: UIButton!
#IBOutlet var P1button6: UIButton!
#IBOutlet var P1button7: UIButton!
#IBOutlet var P1button8: UIButton!
#IBOutlet var P1button9: UIButton!
Second set of buttons;
#IBOutlet var P2button1: UIButton!
#IBOutlet var P2button2: UIButton!
#IBOutlet var P2button3: UIButton!
#IBOutlet var P2button4: UIButton!
#IBOutlet var P2button5: UIButton!
#IBOutlet var P2button6: UIButton!
#IBOutlet var P2button7: UIButton!
#IBOutlet var P2button8: UIButton!
#IBOutlet var P2button9: UIButton!
In my code I apply an Image to one random P1button from an Array;
var buttons = [P1button1, P1button2, P1button3, P1button4, P1button5, P1button6, P1button7, P1button8, P1button9]
buttons.shuffleInPlace()
buttons[0].setImage(UIImage(named: "Green"), forState: .Normal)
Now, I want to know if there is a way to make the P2buttons "co-operate" with the P1buttons. Meaning that if an Image is applied to P1button1 then P2button1 is set to the same image! (and so forth for all buttons)
Example;
P2button1.setImage = UIImage(P1button1)
This obviously doesn't work but hopefully gives you a clue about what I'm trying to achieve.
One possible approach would be by subclassing UIButton. Perhaps something like this...
class TwinButton {
weak var twin: TwinButton?
}
Now, in your viewDidLoad method, link up all of the twins.
p1button1.twin = p2button1
p2button1.twin = p1button1
Now, I'd write a set of methods that have some "with twin" semantics...
extension TwinButton {
setWithTwin(title: String)
setWithTwin(backgroundImage: UIImage)
}
and those methods simply set the value on self and twin.

Resources