2015-02-23 24 views
1

我正在嘗試爲我的應用創建書籤頁。基本上,我想能夠在我的UIViewController中點擊一個按鈕,並將我當前正在查看的網站的網址保存爲UITableViewController中的單元格。問題是我不太確定實現這一點的最佳方式。xcode6中的書籤頁 - Objective-C

我認爲做這將是在我的tableviewcontroller一個可變的數組,我在這裏創建方式:

#import <UIKit/UIKit.h> 

@interface FavoriteViewController : UITableViewController 

@property (strong, atomic) NSMutableArray *tableItems; 

@end 

然後我可以用在的viewController按鈕來填充數組。但是,當我嘗試這樣的事情:

- (IBAction)fave:(id)sender { 
[FavoriteViewController.tableItems addObject:[NSString self.faveURL]]; 
} 

我收到一個錯誤,在FavoriteViewController上找不到屬性tableItems。不知道這是爲什麼。任何人都有解決方案?

回答

0

通過對象訪問屬性,而不是通過類名直接調用。

- (IBAction)fave:(id)sender { 
     // If accessing from other class 
     FavoriteViewController *favVC = ------ 
     [favVC.tableItems addObject:[NSString self.faveURL]]; 
    } 

- (IBAction)fave:(id)sender { 
     // If accessing from same class where you defined your variable 
     [self.tableItems addObject:[NSString self.faveURL]]; 
} 

如果你想訪問使用類名,那麼你必須靜態/類方法,而不是屬性。

+0

好的。我嘗試了第一個選項,但我的FavoriteViewController似乎不受按鈕按下的影響。我猜是因爲按鈕按下隻影響FavoriteViewController的一個實例,並且該實例在我的應用程序中不是同一個實例。有任何解決這個問題的方法嗎?我需要使用靜態方法嗎? – planner15 2015-02-23 17:23:17

+0

你需要添加更多的代碼,不清楚你在問什麼/ – rishi 2015-02-24 06:09:35

+0

沒關係,我想通了我的問題。 – planner15 2015-02-24 20:05:03

0
[self.tableItems addObject:[NSString self.faveURL]]; 

您正在使用當前代碼訪問類變量,但tableItems是一個實例變量。因此,如果函數fave位於FavoriteViewController類中,則在訪問實例變量時需要引用self