はじめまして。
全く関係のない(親子関係ではない)他のViewControllerの値を変更したいですが、うまく変更させることができません。
ご教授いただけますでしょうか。
【前提】
FirstViewController(FirstVC)にはscrollView上に乗っているteamIcon(UIButton)があり、自身のボタン画像としてrealm内に保存されたデータ(NSData)を取得している。
また自身をタップするとカメラロールを開き、画像を選択後、realmに保存することができる。
一方、SecondViewController(SecondVC)にも、teamIcon(UIImageView)があり、realm内に保存されているデータ(上記、FirstVCで述べているデータと同じもの)を表示している。
【やりたいこと】
FirstVCのteamIcon(UIButton)をタップ、realmに保存後、SecondVCのteamIcon(UIImageView)の画像も、そのタイミングで変更する。
【現状】
FirstVCのteamIcon(UIButton)をタップ後、AppDelegateを通じて、SecondVCのteamIconを変更するための関数の呼び出しは成功している模様(print()が動いていることから推測)。
だが、その関数内の teamIcon.image がうまく効いてくれない。
お手数をおかけしますが、アドバイス頂けると幸いです。
宜しくお願い致します。
FirstVC
import UIKit
import Photos
import AssetsLibrary
import RealmSwift
class FirstViewController: UIViewController, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var memberCollectionView : UICollectionView!
var memberCollectionRetsu : Int = Int()
var memberCollectionHeight : CGFloat = CGFloat()
var threadCollectionView : UICollectionView!
@IBOutlet weak var teamCreateView: TeamCreateView!
@IBOutlet weak var makeNewThreadView: MakeNewThreadView!
var memberLabel : UILabel = UILabel()
var teamIcon : UIButton = UIButton()
let plusLabel : UILabel = UILabel()
let threadMakeButton : UIButton = UIButton()
let scrollView : UIScrollView = UIScrollView()
var teamImage : UIImageView = UIImageView()
let editOwnTeamButton : UIButton = UIButton()
let noTeamView : UIView = UIView()
let personalType = PersonalData()
let playerName = PersonalData()
let teamName = TeamData()
var teamImageName = TeamData()
var testView : UIImageView = UIImageView()
var teamNameLabel : UILabel = UILabel()
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
var teamNameLabelYoko = CGFloat()
var teamNameCountInt = Int()
var teamNameCount = CGFloat()
var teamNameSize = CGFloat()
let maskingImage = UIImage(named: "[email protected]")
let imageTest : UIImageView = UIImageView()
let teamContetnsSettingView : UIView = UIView()
//ここから、iconimageの定義
//タグ付けして判定。1ならnewiconimageに保存
let newIconImage : UIImageView = UIImageView()
//アイコン画像用のimagetest
var imagetest4Icon : UIImageView = UIImageView()
//アイコン画像用のnewTeamImage
var newTeamImage4Icon : NSData = NSData()
//ここまで、iconimageの定義
var newTeamImage : NSData = NSData()
let topColorLabel = UILabel()
let subBlueLabel : UILabel = UILabel()
let blueCircle = CAShapeLayer()
//let circleImage = UIImageView()
var pngData : NSData = NSData()
let nsdataTeamImg : UIImage = UIImage()
let backHideViewButton : UIButton = UIButton()
let teamNameEnterTextField : UITextField = UITextField()
let backView : UIView = UIView()
let frameSukima : CGFloat = 20.0
var isObserving = false
var teamActivePlace = TeamData()
var teamImage4Realm = TeamData()
var testInt2 = TeamData()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let realm = try! Realm()
scrollView.addSubview(testView)
if teamName.teamName == ""{
} else {
teamImage.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 155.0)
if let loadTeamImage = realm.objects(TeamData).first?.teamImageData {
teamImage.image = UIImage(data: loadTeamImage)
}
// let testimagedata = UIImage(data: teamImageName.teamImageData!)
// teamImage.image = testimagedata
teamImage.contentMode = .ScaleToFill
scrollView.addSubview(teamImage)
//チーム設定しない時の設定
if teamName.teamName == ""{
teamImage.hidden = true
} else {
teamImage.hidden = false
}
}
teamNameCountInt = teamName.teamName.characters.count
teamNameCount = CGFloat(teamNameCountInt)
teamNameLabelYoko = self.view.bounds.width - 20
teamNameSize = CGFloat()
if teamNameCount * 21 >= teamNameLabelYoko{
teamNameSize = teamNameLabelYoko / teamNameCount
} else {
teamNameSize = 21
}
//ここから、チーム名のラベルの設定
teamNameLabel.frame = CGRectMake(0,0,teamNameLabelYoko,teamNameSize)
if let loadTeamName = realm.objects(TeamData).first?.teamName {
teamNameLabel.text = "\(loadTeamName)"
}
teamNameLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: statusBarHeight + 30)
teamNameLabel.textAlignment = NSTextAlignment.Center
teamNameLabel.font = UIFont(name: "HiraginoSans-W3", size: teamNameSize)
teamNameLabel.textColor = UIColor.whiteColor()
scrollView.addSubview(teamNameLabel)
//ここまで、チーム名のラベルの設定
//チーム設定しない時の設定
if teamName.teamName == ""{
teamNameLabel.hidden = true
} else {
teamNameLabel.hidden = false
}
let teamIconBack : UIImageView = UIImageView()
teamIconBack.frame = CGRect(x: self.view.bounds.width/2 - 75 - 2, y: statusBarHeight + 23 - 2, width: 61.0, height: 61.0)
teamIconBack.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 1.25 - 41.5,y: statusBarHeight + 23 - 1 + 118.5 + 10)
teamIconBack.image = UIImage(named: "[email protected]")
scrollView.addSubview(teamIconBack)
//チーム設定しない時の設定
if teamName.teamName == ""{
teamIconBack.hidden = true
} else {
teamIconBack.hidden = false
}
if let loadIconImage = realm.objects(TeamData).first?.teamIconData {
teamIcon.setImage(maskImage(UIImage(data: loadIconImage)!, mask: maskingImage!), forState: .Normal)
}
teamIcon.addTarget(self, action: "selectTeamIcon:", forControlEvents: .TouchUpInside)
teamIcon.frame = CGRect(x: self.view.bounds.width/2 - 75, y: statusBarHeight + 23, width: 55.0, height: 55.0)
teamIcon.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 42.5,y: statusBarHeight + 23 + 117.5 + 10)
scrollView.addSubview(teamIcon)
if teamName.teamName == ""{
teamIcon.hidden = true
} else {
teamIcon.hidden = false
}
let uenotakasa : CGFloat = 260.0 + 100.0
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
let tabbarHeight : CGFloat = 40.0
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, uenotakasa + memberCollectionHeight + tabbarHeight)
scrollView.bounces = false
self.view.addSubview(scrollView)
teamContetnsSettingView.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
teamContetnsSettingView.backgroundColor = UIColor(white: 0.05, alpha: 0.7)
self.view.addSubview(teamContetnsSettingView)
teamContetnsSettingView.hidden = true
backView.layer.position = CGPoint(x: UIScreen.mainScreen().bounds.width/2, y: UIScreen.mainScreen().bounds.height/2)
backView.layer.cornerRadius = 7
backView.layer.borderWidth = 0.8
backView.layer.borderColor = UIColor.darkGrayColor().CGColor
backView.backgroundColor = UIColor.whiteColor()
//self
teamContetnsSettingView.addSubview(backView)
var kariireStr = "中身なし";
newTeamImage = kariireStr.dataUsingEncoding(NSUTF8StringEncoding)!
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// スクリーンの編集以外の部分をタップしたらedit状態を外す
@IBAction func tapScreen(sender: AnyObject) {
self.view.endEditing(true)
}
//ここから、画像マスキング
func maskImage(image:UIImage, mask:(UIImage))->UIImage{
let imageReference = image.CGImage
let maskReference = mask.CGImage
let imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
CGImageGetHeight(maskReference),
CGImageGetBitsPerComponent(maskReference),
CGImageGetBitsPerPixel(maskReference),
CGImageGetBytesPerRow(maskReference),
CGImageGetDataProvider(maskReference), nil, true)
let maskedReference = CGImageCreateWithMask(imageReference, imageMask)
let maskedImage = UIImage(CGImage:maskedReference!)
return maskedImage
//ここまで、画像マスキング
}
//teamiconを選択する時の挙動
func selectTeamIcon(sender: AnyObject){
newIconImage.tag = 1
openAlbum1()
writeNewTeamIcon4Realm()
}
func writeNewTeamIcon4Realm(){
appDelegate.secondVC.testtest()
}
func openAlbum(sender: UIButton) {
openAlbum1()
}
func openAlbum1(){
var ipc: UIImagePickerController = UIImagePickerController();
ipc.delegate = self
UIImagePickerControllerSourceType.PhotoLibrary
self.teamContetnsSettingView.window?.rootViewController!.presentViewController(ipc, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
// アルバム画面を閉じます
picker.dismissViewControllerAnimated(true, completion: nil);
// 画像をリサイズしてUIImageViewにセット
var resizeImage = resize(image, width: Int(UIScreen.mainScreen().bounds.width), height: 155)
if newIconImage.tag == 1{
// 画像をリサイズしてUIImageViewにセット
var resizeImage = resize(image, width: Int(UIScreen.mainScreen().bounds.width), height: 155)
self.teamIcon.setImage(maskImage(image!, mask: maskingImage!), forState: .Normal)
print("resize_newicon")
} else {
// 画像をリサイズしてUIImageViewにセット
var resizeImage = resize(image, width: Int(UIScreen.mainScreen().bounds.width), height: 155)
self.teamImage.image = resizeImage
print("resize_teamimage")
}
}
// 画像をリサイズ
func resize(image: UIImage, width: Int, height: Int) -> UIImage {
var imageRef: CGImageRef = image.CGImage!
var sourceWidth: Int = CGImageGetWidth(imageRef)
var sourceHeight: Int = CGImageGetHeight(imageRef)
var size: CGSize = CGSize(width: width, height: height)
UIGraphicsBeginImageContext(size)
image.drawInRect(CGRectMake(0, 0, size.width, size.height))
var resizeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resizeImage
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: [String: AnyObject]) {
if didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] != nil {
if newIconImage.tag == 1{
imagetest4Icon.image = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as? UIImage
newTeamImage4Icon = UIImagePNGRepresentation(imagetest4Icon.image!)!
print("hiugigiugu\(newTeamImage4Icon.length)")
teamIcon.setImage(maskImage(imagetest4Icon.image!, mask: maskingImage!), forState: .Normal)
let realm = try! Realm()
let team = realm.objects(TeamData).last!
try! realm.write {
team.teamIconData = newTeamImage4Icon
}
} else {
imageTest.image = didFinishPickingMediaWithInfo[UIImagePickerControllerOriginalImage] as? UIImage
newTeamImage = UIImagePNGRepresentation(imageTest.image!)!
}
}
//写真選択後にカメラロール表示ViewControllerを引っ込める動作
picker.dismissViewControllerAnimated(true, completion: nil)
}
}
SecondVC
import UIKit
import Photos
import AssetsLibrary
import RealmSwift
class SecondViewController: UIViewController, UIScrollViewDelegate{
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet weak var _editButtonView: EditButtonView!
//
@IBOutlet weak var twitterSettingView: TwitterSettingView!
let scrollView : UIScrollView = UIScrollView()
let twitterName = PersonalData()
var personalType = PersonalData()
var playerName = PersonalData()
var message = PersonalData()
var teamName = TeamData()
let keyboardToolBar = UIToolbar()
let tableView : UITableView = UITableView()
var nameLabel: UILabel = UILabel()
var teamIcon : UIImageView = UIImageView()
let inputHeight : CGFloat = 248.0
var currentPoint : CGFloat = CGFloat()
var teamNameLabel : UILabel = UILabel()
var passedName : String = "データないよ"
//-----------------------------------------------------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
//ここから、タブバーのアイコンの設定
let tabImage = UIImage(named: "[email protected]")
let tabSelectedImage = UIImage(named: "[email protected]")
let attributes = [NSFontAttributeName:UIFont(name: "HiraKakuProN-W3", size: 9)]
self.tabBarItem.image = tabImage
self.tabBarItem.selectedImage = tabSelectedImage
self.tabBarItem.title = "プロフィール"
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
//ここまで、タブバーのアイコンの設定
// //ここから、Navigationbarの編集
// //ここから、すべてのNavigationbarに共通する設定編集
navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
navigationController?.navigationBar.shadowImage = UIImage()
// //ここまで、すべてのNavigationbarに共通する設定編集
// //ここまで、Navigationbarの編集
let uenotakasa : CGFloat = 300
let twitterKanrenHeight : CGFloat = 80.0
let wHeight : CGFloat = 200.0
let tabbarHeight : CGFloat = 49.0 //49
//ステータスバーの高さ
let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
//ナビゲーションバーの高さ
let navBarHeight = self.navigationController?.navigationBar.frame.size.height
//高さは、ここ基準
let basicHeight = statusBarHeight + navBarHeight!
//スクリーンの横幅
let screenWidth = self.view.bounds.width
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - tabbarHeight)
if teamName == ""{
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, uenotakasa + twitterKanrenHeight + wHeight - 90.0)
} else {
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, uenotakasa + twitterKanrenHeight + wHeight)
}
scrollView.bounces = false
scrollView.delegate = self
scrollView.backgroundColor = UIColor(red:245/255, green:245/255, blue:245/255, alpha:1.0)
view.addSubview(scrollView)
let teamImageSlim : UIImageView = UIImageView()
teamImageSlim.frame = CGRect(x: 0, y: statusBarHeight + navBarHeight! + 3, width: self.view.bounds.width, height: 90.0)
if let loadTeamImage = realm.objects(TeamData).first?.teamImageData {
teamImageSlim.image = UIImage(data: loadTeamImage)
}
scrollView.addSubview(teamImageSlim)
//チーム設定しない時の設定
if teamName == ""{
teamImageSlim.hidden = true
} else {
teamImageSlim.hidden = false
}
let teamLabel = UILabel(frame: CGRectMake(0,0,40,12))
teamLabel.layer.position = CGPoint(x: self.view.bounds.width/2 + 10,y: basicHeight + 35)
teamLabel.font = UIFont(name: "HiraginoSans-W6", size: 12)
teamLabel.textColor = UIColor.whiteColor()
teamLabel.text = "チーム"
scrollView.addSubview(teamLabel)
//チーム設定しない時の設定
if teamName == ""{
teamLabel.hidden = true
} else {
teamLabel.hidden = false
}
let teamNameCountInt : Int = teamName.teamName.characters.count
let teamNameCount : CGFloat = CGFloat(teamNameCountInt)
let teamNameLabelYoko : CGFloat = self.view.bounds.width/2 - 10
var teamNameSize = CGFloat()
if teamNameCount * 20 >= teamNameLabelYoko{
teamNameSize = teamNameLabelYoko / teamNameCount
} else {
teamNameSize = 20
}
teamNameLabel.frame = CGRectMake(0,0,teamNameLabelYoko,teamNameSize)
teamNameLabel.text = teamName.teamName
// teamNameLabel.layer.position = CGPoint(x: self.view.bounds.width/2 + teamNameSize,y: basicHeight + 55)
teamNameLabel.layer.position = CGPoint(x: self.view.bounds.width/2 + teamNameLabelYoko/2 - 10,y: basicHeight + 55)
teamNameLabel.textAlignment = NSTextAlignment.Left
teamNameLabel.font = UIFont(name: "HiraginoSans-W3", size: teamNameSize)
teamNameLabel.textColor = UIColor.whiteColor()
scrollView.addSubview(teamNameLabel)
//ここまで、ラベルの設定
//チーム設定しない時の設定
if teamName == ""{
teamNameLabel.hidden = true
} else {
teamNameLabel.hidden = false
}
//ここから、チームアイコンの設定
//ここから、アイコンの背景画像の設定
let teamIconBack : UIImageView = UIImageView()
teamIconBack.frame = CGRect(x: self.view.bounds.width/2 - 75 - 2, y: statusBarHeight + navBarHeight! + 23 - 2, width: 59.0, height: 59.0)
teamIconBack.image = UIImage(named: "[email protected]")
scrollView.addSubview(teamIconBack)
//チーム設定しない時の設定
if teamName == ""{
teamIconBack.hidden = true
} else {
teamIconBack.hidden = false
}
let maskingImage = UIImage(named: "[email protected]")
if let loadIconImage = realm.objects(TeamData).first?.teamIconData {
teamIcon.image = maskImage(UIImage(data: loadIconImage)!, mask: maskingImage!)
}
teamIcon.frame = CGRect(x: self.view.bounds.width/2 - 75, y: statusBarHeight + navBarHeight! + 23, width: 55.0, height: 55.0)
scrollView.addSubview(teamIcon)
if teamName == ""{
teamIcon.hidden = true
} else {
teamIcon.hidden = false
}
nameLabel.frame = CGRectMake(0,0,60,30)
if teamName == ""{
nameLabel.layer.position = CGPoint(x: 115,y: 120)
} else {
nameLabel.layer.position = CGPoint(x: 126,y: 207)
}
nameLabel.text = playerName.playerName
nameLabel.font = UIFont(name: "HiraginoSans-W3", size: 15)
nameLabel.textColor = UIColor.darkGrayColor()
//文字間の設定
let attributedText = NSMutableAttributedString(string: playerName.playerName)
//文字間の値
let customLetterSpacing = 1
//文字間(NSKernAttributeName)を設定する
attributedText.addAttribute(NSKernAttributeName, value: customLetterSpacing, range: NSMakeRange(0, attributedText.length))
//textではなく、attributedTextを使う
nameLabel.attributedText = attributedText
scrollView.addSubview(nameLabel)
//ここまで名前のラベル設定
let personalIconBack : UIImageView = UIImageView()
personalIconBack.frame = CGRect(x: 0, y: 0, width: 67.0, height: 67.0)
personalIconBack.image = UIImage(named: "[email protected]")
if teamName == ""{
personalIconBack.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 1.25 - 41.5,y: statusBarHeight + navBarHeight! + 23 - 1 + 29.5)
} else {
personalIconBack.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 1.25 - 41.5,y: statusBarHeight + navBarHeight! + 23 - 1 + 118.5)
}
scrollView.addSubview(personalIconBack)
//ここまで、アイコンの背景画像の設定
//アイコン画像の設定(viewdidload外に、マスキングのためのfuncあり。)
let personalIcon : UIImageView = UIImageView()
let personalImage = UIImage(named: "[email protected]")
let personalMaskingImage = UIImage(named: "[email protected]")
personalIcon.image = maskImage(personalImage!, mask: personalMaskingImage!)
personalIcon.frame = CGRect(x: 0, y: 0, width: 65.0, height: 65.0)
if teamName == ""{
personalIcon.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 42.5,y: statusBarHeight + navBarHeight! + 23 + 28.5)
} else {
personalIcon.layer.position = CGPoint(x: self.view.bounds.width/2 - 75 - 42.5,y: statusBarHeight + navBarHeight! + 23 + 117.5)
}
scrollView.addSubview(personalIcon)
}
func testtest(){
let realm = try! Realm()
if let loadIconImage = realm.objects(TeamData).first?.teamIconData {
let maskingImage = UIImage(named: "[email protected]")
teamIcon.image = maskImage(UIImage(data: loadIconImage)!, mask: maskingImage!)
}
print("OK")
}
//ここまで、tableviewのfuncの設定
//ここから、画像マスキング
func maskImage(image:UIImage, mask:(UIImage))->UIImage{
let imageReference = image.CGImage
let maskReference = mask.CGImage
let imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
CGImageGetHeight(maskReference),
CGImageGetBitsPerComponent(maskReference),
CGImageGetBitsPerPixel(maskReference),
CGImageGetBytesPerRow(maskReference),
CGImageGetDataProvider(maskReference), nil, true)
let maskedReference = CGImageCreateWithMask(imageReference, imageMask)
let maskedImage = UIImage(CGImage:maskedReference!)
return maskedImage
//ここまで、画像マスキング
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
AppDelegate
//
// AppDelegate.swift
//
import UIKit
import RealmSwift
import CVCalendar
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//ここから、値の受け渡しに関する設定
var selectedYear : Int = Int()
var selectedMonth : Int = Int()
var selectedDay : Int = Int()
//var selectedCalendar : CVDate = CVDate!()
var secondVC = SecondViewController()
//ここまで、値の受け渡しに関する設定
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
//realmマイグレーション(省略)
let config = Realm.Configuration(
})
secondVC.passedName = "fafae"
secondVC.testtest()
return true
}
}
【storyboad経由でのSecondVC.swiftで生成したオブジェクトへのアクセス】
下記、secondVC.teamIcon.image ...としたところで、secondVCにはteamIconがありません(no member)とでます。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondVC = storyboard.instantiateViewControllerWithIdentifier("secondViewCon")
secondVC.teamIcon.image ...
secondVC
と表示しているビューコントローラが別ではないかと思います。コードを省略せずに全部掲載するか、プロジェクトを丸ごと、どこかに共有してもらえませんか?@IBOutlet
などがあるのでStoryboardを使われているのだと思います。その場合、画面に表示されているSecondVCのオブジェクトはStoryboardから自動的に生成されるので、AppDelegeteのプロパティでvar secondVC = SecondViewController()
としても別のオブジェクトなので表示されている画面には何の影響も与えません。この点は理解されているでしょうか?変更通知に関してですが、Realmを使っていて、Realmにオブジェクトを保存しているのですから、Realmの通知を利用してSecondVCで再度オブジェクトを引き直すのがいいと思います。storyboard.instantiateViewControllerWithIdentifier
)を使ってインスタンスを作成しても同じ内容の「別のオブジェクト」ができるだけですので結果は変わりません。まずクラス(Class, Struct, Enumなど)とインスタンスの関係について理解が曖昧なようなので、再度学習することをお勧めします。異なるクラス間で直接メソッドを呼び出したりしたい場合には、互いのオブジェクトの参照を保持する必要があります。そういう意味ではAppDelegateにプロパティとしてSecondViewControllerを保持しているのは合っているのですが、プロパティの中身に別のオブジェクトが入ってしまっているのが問題です。そのプロパティには画面に表示されているSecondViewControllerのオブジェクトを入れなければなりません。当初と質問の意図が変わってきていますので、おっしゃる通り、別の質問を作成した方が良いと思います。