2017-10-17 12 views
0
- (void) tapGesture: (id)sender 
{ 
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 
    NSInteger userID = gesture.view.tag; 

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" 
                 bundle:nil]; 
    OthersProfile *vc = (OthersProfile*)[storyboard instantiateViewControllerWithIdentifier:@"othersprofile"]; 
    NSString *strUserID = [NSString stringWithFormat:@"%ld", (long)userID]; 

    vc.userID = strUserID; 
    [self.viewController.navigationController pushViewController:vc 
                 animated:YES]; 
} 

Il travaillait avec Xcode 8/Swift 3, mais semble comme il y a un problème avec Xcode 9 et rapide 4.accès à une propriété rapide de objc

J'ai USERID propriété introuvable erreur.

Dans le fichier Swift:

var userID : String = "" 
override func viewDidLoad() { 
    print(userID) //no value here 
} 

Toute personne ayant une idée?

+0

Etes-vous sûr que ce code fonctionnait dans XCode8? –

+0

@PuneetSharma désolé j'ai mis à jour ma question –

+0

Swift 4 n'expose plus tout à l'exécution d'Objective-C, donc vous pouvez devoir annoter la propriété avec '@ objc' – ctietze

Répondre

2

Essayez ceci:

@objc var userID : String = " 

Les règles pour exposer le code Swift à Objective-C ont changé à Swift 4. En tant que tel, votre code est correct dans Swift 3 et donc la confusion que vous faites face;)

Le nouveau la conception est entièrement détaillée dans the corresponding Swift Evolution proposal.

+0

@UtkuDalmaz ... ceci est, malheureusement, un problème * très * commun lors de la migration de Swift 3 à 4 :-( –

0

Addression votre autre question que vous devez fixer votre mode d'accès contrôleur:

En effet, vc.user est nulle lorsque vous essayez de le définir, ajouter une variable à commande rapide et le mettre à la place de `vc.user. texte

vc.yourVariable = strUserID; 
[self.viewController.navigationController pushViewController:vc 
                animated:YES]; 

puis à viewDidAppear vous pourrez le régler avec cette variable

@IBOutlet var user: UILabel! 
public var yourVariable: String = "" 

override func viewDidLoad() { 
    user.text = yourVariable 
    print(user.text!) //should be value 

} 
+0

J'ai essayé ceci mais cette fois j'ai la propriété 'userID' pas trouvé sur l'objet de type 'OthersProfile *'? Erreur. Vérifiez mon autre question https://stackoverflow.com/questions/46754687/cannot-access-a-variable-of-other-view/46755098?noredirect=1#comment80464520_46755098 –

+0

avez-vous importer votre fichier pour voir cette variable? –

+0

Je n'ai pas compris votre question? –