2014-04-04 72 views
0

我正在嘗試從我的類中的類PYTimelineViewController中檢索NSString類型的變量。從另一個類中檢索變量 - 目標C

這裏我在做什麼:

在我rippleMake.m

/* SET OBJECT */ 
    PYTimelineViewController *getRippleForVideo = [[PYTimelineViewController alloc] init]; 
    /* ESTABLISH THE RIPPLE IT VIDEO FOR CHALLENGE VIDEO */ 
    NSString *videoFor = [getRippleForVideo getchallengeForVideo]; 
    NSLog(@"%@",videoFor); 

在運行此,videoFor返回null

在我PYTimelineViewController.h

@interface PYTimelineViewController : UITableViewController//<MWPhotoBrowserDelegate> 

... 
@property (nonatomic, strong) NSString *challengeForVideo; 
-(NSString *)getchallengeForVideo; 
- (void)setchallengeForVideo:(NSString*)challengeforVideo; 
@end 
PYTimelineViewController.m

-(IBAction)openRippleIt:(id)sender 
{ 
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; 
    NSInteger rowOfTheCell = (indexPath.row + 1000); // ADD 1000 to get away from effecting other code 
    UIButton *button=(UIButton *)[self.view viewWithTag:rowOfTheCell]; 

    NSString *btnTitle = [button currentTitle]; 
    [self setchallengeForVideo:btnTitle]; 
    NSLog(@"%@",[self getchallengeForVideo]); <--- This outputs exactly what I need. 
    [self performSegueWithIdentifier:@"moveTorippleIt" sender:self]; 
} 

.m getter和setter

-(void) setchallengeForVideo:(NSString *)challengeforVideo 
{ 
    _challengeForVideo = challengeforVideo; 
} 
-(NSString *) getchallengeForVideo 
{ 
    return _challengeForVideo; 
} 

這是粗推動用戶對新ViewController但中不應該是一個問題。

不知道爲什麼我無法檢索值,因爲一切似乎都已正確配置。特別是我的getters和setters。

建議,想法?

+0

什麼時候被稱爲'openRippleIt'設置'challengeForVideo'? – Larme

+1

請注意,getters和setters會爲您生成屬性。您可以完全刪除getchallengeForVideo和setchallengeForVideo:方法。 – drewag

+0

@CrimsonChris在正確的軌道上,你需要以某種方式引用先前的'PYTimelineViewController'而不是創建一個新的。 – Aaron

回答

1

如果您想要從視圖控制器中檢索一個對象,可以簡單地將對象發送給你的類。

當你實例化PYTimelineViewControllerrippleMake.m,這是一個完全新的,所以你鬆散想要的對象的引用。

另外,如果您在該點的唯一目標是檢索var的值,則無需實施吸氣劑或設置器@property鍵爲您創建getter/setter。

傳遞對象到視圖通過這一個加入公共財產

// RippleMake.h 
// e.g 
@property (strong, nonatomic) NSString *foo 

當你實例化你的視圖控制器類,一定要經過物業:

// ViewController.m 
// e.g 
RippleMake *ripple = .... 
ripple.foo = foo; 
... 

要通過對象視圖控制器到另一個,你必須只是做同樣的事情,但在prepareForSegue:sender方法,看到這樣的回答:https://stackoverflow.com/a/22867976/1745596

+0

完美!非常感謝。 –

1

其他一些評論...

如果你使用的Xcode> = 4.4不需要getter和setter方法。它們由編譯器自動創建,以響應@property

習慣用法Objective-C不在屬性上使用「get」前綴。

「複製」通常是用於字符串的正確屬性屬性,因爲它們可以是可變的。

你的頭應該是這樣的:

@interface PYTimelineViewController : UITableViewController 
@property (nonatomic, copy) NSString *challengeForVideo; 
@end 

用法應該是這樣的:

PYTimelineViewController *vc = [[PYTimelineViewController alloc] init]; 
[vc setChallengeForVideo:@"some text"]; 
NSLog(@"%@", [vc challengeForVideo]); 

獎勵積分,使用,而不是點語法:

PYTimelineViewController *vc = [[PYTimelineViewController alloc] init]; 
vc.challengeForVideo = @"some text"; 
NSLog(@"%@", vc.challengeForVideo); 
+0

感謝您的信息。真的有幫助! –