如何从swift中的另一个类访问变量?

时间:2017-08-29 11:54:14

标签: ios swift

我的 UserSearchController

中有一个searchBar
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
}

2 个答案:

答案 0 :(得分:0)

您需要通过对象引用而不是类名来访问该变量。试试这个 -

userProfileController.searchBar.isHidden = true

答案 1 :(得分:0)

你可以这样做 我在这里传递数组。但您可以根据需要传递任何变量类型。

arr_sticker是您的推送控制器, let SV = self.storyboard?.instantiateViewController(withIdentifier: "ShareViewController") as! ShareViewController SV.arr_sticker = arrm_symbols self.navigationController?.pushViewController(SV, animated: true) 是您要传递的任何变量。记住传递变量和分配变量类型都是相同的。

    var arr_sticker = NSArray()

在另一个控制器中,我有像

这样的十进制数组
print

在此arr_sticker CREATE FUNCTION SPLIT_STR( x VARCHAR(255), delim VARCHAR(12), pos INT ) RETURNS VARCHAR(255) RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos), LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1), delim, ''); 值为SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third; 的情况下,您可以获得实际从VC传递给此VC的值。