質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

1986閲覧

appdelegateを使用して、2画面で変数を使いたい

afuroda

総合スコア36

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/11/10 18:24

###前提・実現したいこと
multipeerconectivityを使用して2端末間で通信を行いたいのですが、通信を確立させた後に、画面遷移を行って、遷移先の画面でデータのやり取りを行いたくて、MCSessionの変数をappdelegateに書いたのですが、Viewcontroller.swiftにエラーが出てきて困っています。
どなたか教えてください。
###発生している問題・エラーメッセージ

Ambiguous reference to member 'session(_:peer:didChangeState:)'

###該当のソースコード

swift

1 2import UIKit 3import MultipeerConnectivity 4class ViewController: UIViewController,MCSessionDelegate,MCNearbyServiceBrowserDelegate,MCNearbyServiceAdvertiserDelegate { 5 6 let peer : MCPeerID = MCPeerID(displayName: UIDevice.currentDevice().name) 7 8 var browser : MCNearbyServiceBrowser? 9 var advertis : MCNearbyServiceAdvertiser? 10 let service = "p2p" 11 var appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate 12 13 override func viewDidLoad() { 14 //self.session = MCSession(peer: peer) 15 //self.session!.delegate = self 16 self.browser = MCNearbyServiceBrowser(peer: peer, serviceType: service) 17 self.browser!.delegate = self 18 self.advertis = MCNearbyServiceAdvertiser(peer: peer, discoveryInfo: nil, serviceType: service) 19 self.advertis?.delegate = self 20 super.viewDidLoad() 21 22 } 23 24 override func didReceiveMemoryWarning() { 25 super.didReceiveMemoryWarning() 26 } 27 28 @IBAction func browser(sender: AnyObject) { 29 30 self.browser!.startBrowsingForPeers() 31 } 32 33 @IBAction func advertiser(sender: AnyObject) { 34 self.advertis!.startAdvertisingPeer() 35 } 36 func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) { 37 print("[MCNearbyServiceBrowserDelegate] browser:foundPeer:withDiscoveryInfo:") 38 print(" + peerID = \(peerID.displayName)") 39 print(" + info = \(info)") 40 //browser.invitePeer(peerID, toSession: self.session!, withContext: nil, timeout: 10) 41 } 42 43 func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) { 44 print("[MCNearbyServiceBrowserDelegate] browser:lostPeer:") 45 print(" + peerID = \(peerID.displayName)") 46 } 47 48 49 func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void) { 50 print("[MCNearbyServiceAdvertiserDelegate] advertiser:didReceiveInvitationFromPeer:withContext:invitationHandler:") 51 print(" + peerID = \(peerID.displayName)") 52 53 let alert = UIAlertController(title: "招待を受けました", message: "from \(peerID.displayName)", preferredStyle: .Alert) 54 alert.addAction(UIAlertAction(title: "参加", style: .Default) { action in 55 //invitationHandler(true, self.session!) 56 }) 57 alert.addAction(UIAlertAction(title: "拒否", style: .Cancel) { action in 58 //invitationHandler(false, self.session!) 59 }) 60 self.presentViewController(alert, animated: true, completion: nil) 61 } 62 func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) { 63 let viewcontroller = self.storyboard!.instantiateViewControllerWithIdentifier("view2") 64 self.presentViewController(viewcontroller,animated: true,completion: nil) 65 print("change window") 66 67 } 68 func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) { 69 print("receivedData") 70 } 71 72 func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) { 73 } 74 75 func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) { 76 } 77 78 func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) { 79 } 80 81} 82
import UIKit import MultipeerConnectivity @UIApplicationMain class AppDelegate: UIResponder,UIApplicationDelegate { var window: UIWindow? var session : MCSession? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }

###試したこと
インターネットで方法を探しましたが、わかりませんでした

###補足情報(言語/FW/ツール等のバージョンなど)
プログラム文は上がViewcontroller.swift
下がappdelegate.swiftです。
コメントアウトしている場所にエラーが出ています。
エラー内容は同じです。
わかりにくいかもしれませんが宜しくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

self.sessionでは自クラスのsessionへのアクセスになっていまいますので、

swift

1let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

として、AppDelegateを取得した後に、appDelegate.sessionでアクセスして下さい。

‥と、ここまで書いて、

swift

1var appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate

というコードがあることに気が付きました‥。

self.sessionではなく、appdelegate.sessionでアクセスして下さい。

投稿2016/11/10 23:53

編集2016/11/11 00:13
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

afuroda

2016/11/11 04:53

解決しました。ありがとうございます! selfの意味が曖昧なまま使用していました(´・ω・`) 勉強になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問