2017-08-29 20 views
0

私は私のUserSearchControllerに検索バーがあります。別のクラスの変数に迅速にアクセスするにはどうすればいいですか?

class UserSearchController: UICollectionViewController, UICollectionViewDelegateFlowLayout,UISearchBarDelegate, UISearchDisplayDelegate { 

lazy var searchBar: UISearchBar = { 
    let sb = UISearchBar() 
    sb.placeholder = "Search" 
    sb.barTintColor = UIColor.gray 
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230, alpha: 1) 
    sb.keyboardAppearance = .dark 
    sb.delegate = self 
    return sb 
}() 

そして私はアクセスしたいとUserSearchCvからの検索バー非表示:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let user = filteredUsers[indexPath.item] 
    print(user.username) 

    let userProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout()) 
    (superview?.next as? UIViewController)?.navigationController?.pushViewController(userProfileController, animated: true) 

    UserSearchController.searchBar.isHidden = true //Something like this but it compiles an error 
} 
+0

これはあなたを助けるかもしれません。 https://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers/2520/using-the-delegate-pattern-passing-data-back#t=201708291155386938448 –

+0

'UserSearchController()。searchBar .isHidden = true'は、コントローラのインスタンスを作成しているので、コンパイルする必要があります。あなたがやっているやり方は、クラスのインスタンスではなく、 'UserSearchController'クラスそのものです。 'UserSearchController'のインスタンスへの参照が必要です。 –

+0

UserSearchCv内にUserSearchControllerのインスタンスを宣言していますか?そのインスタンスを使用してください – 3stud1ant3

答えて

0

はあなたがオブジェクトでその変数にアクセスする必要があるがクラス名の代わりに参照。これを試してください

userProfileController.searchBar.isHidden = true 
+0

ええ私もこれを行ったが、それはエラーにコンパイル –

+0

'UserProfileController'が検索バーのプロパティを持っていない限りコンパイルされているかどうかは分かりません。この質問では、 'UserSearchController'だけが検索バーのプロパティを持っています。 –

+0

@これはもうエラーをコンパイルしませんが、検索バーは消えません。検索バーを変更することはできません –

0

あなたはそうすることができます 私はここでパススルーしています。しかし、任意の変数型を必要に応じて渡すことができます。

ShareViewControllerは、プッシュするコントローラであり、​​はあなたが渡すための任意のバリバリです。変数を渡すことと、変数の型を割り当てることは同じであることを覚えておいてください。あなたは、あなたが実際にこのVCにVCから渡された値を​​のprint値を得ることができたときに

let SV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController 
    SV.arr_sticker = arrm_symbols 
    self.navigationController?.pushViewController(SV, animated: true) 

は別のコントローラでは、私はこのコントローラでは

var arr_sticker = NSArray() 

のような配列をdecalredています。

関連する問題