1

當我選擇返祖 - >說明如果我點擊按鈕等按鈕變爲不像和「象數= 1099」增加發生。如果我按回到我想這不像下一個要顯示返祖讓一個標籤說,然後再次選擇返祖按鈕應顯示不像和象數應1100 請幫助我如何才能做到這一點?點擊像所有視圖應該顯示類似目標C

enter image description here enter image description here

//DetailOfUser.m

#impot"DetailsOfStories.h"  
    @interface DetailOfUser()<UITableViewDelegate,UITableViewDataSource> 
    { 
     NSMutableArray *arrayAboutList; 
     DetailsOfStories *viewController; 
    } 

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cell"; 
    UILabel *title=[(UILabel *)cell viewWithTag:2]; 
    title.text=[NSString stringWithFormat:@"%@", [arrayAboutList[indexPath.row] valueForKey:@"title"]]; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 


    viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailsOfStories"]; 
    viewController.descriptionList = [arrayAboutList[indexPath.row] mutableCopy]; 
    [self.navigationController pushViewController:viewController animated:YES]; 


} 
@end 

//DetailsOfStories.m

#import "DetailOfUser.h" 
@interface DetailsOfStories() 
{ 
    NSMutableArray *descriptionList; 
BOOL buttonToggled; 
    int number,number1; 
    NSNumber *num1; 
} 
@end 
@implementation DetailsOfStories 
@synthesize descriptionList; 
- (void)viewDidLoad { 
    UILabel *like_count=(UILabel *)[self.view viewWithTag:3]; 
    NSNumber *someNumber = @(number); 
    NSString *someString = [someNumber stringValue]; 
    like_count.text=someString; 
} 
- (IBAction)like:(id)sender { 

    if (!buttonToggled) { 
     [sender setTitle:@"Unlike" forState:UIControlStateNormal]; 
     //number is interger 
     number = [num1 intValue]+1; 
     number1=number; 
     UILabel *like_count=(UILabel *)[self.view viewWithTag:3]; 
     NSNumber *someNumber = @(number); 
     NSString *someString = [someNumber stringValue]; 
     like_count.text=someString; 
     buttonToggled = YES; 
    } 
    else { 
     [sender setTitle:@"Like" forState:UIControlStateNormal]; 
     number1 = number1-1; 
     UILabel *like_count=(UILabel *)[self.view viewWithTag:3]; 
     NSNumber *someNumber = @(number1); 
     NSString *someString = [someNumber stringValue]; 
     like_count.text=someString; 
     buttonToggled = NO; 
    } 
} 

回答

0

可以封裝 '喜歡' 數量和狀態到數據模型。
您的控制器都可以訪問和修改數據模型。在這種情況下,數據模型可以是singleton,以便您可以在控制器中獲取它。
可能你需要同步數據到服務器或持久數據到本地存儲以後,數據模型可以封裝所有這些服務。

下面是一些代碼示例

// YourDataModel 
@interface YourDataModel : NSObject 

@property (nonatomic, assign) NSNumber *numbersOfLike; 
@property (nonatomic, assign) BOOL like; 

@end 
@implemention 
+ (id)shareInstance { 
    static dispatch_once_t onceToken; 
    static YourDataModel *model; 
    dispatch_once(&onceToken, ^{ 
    model = [[YourDataModel alloc] init]; 
    }); 
    return model; 
} 

// maybe sync data to backend server. 
- (void)sync { 
} 

// load like numbers from local storage or remote server 
- (instancetype)init { 

} 

// Then you can use it in your controllers 
[[YourDataModel shareInstance] like]; 
[[YourDataModel shareInstance] numbersOfLike]; 
[[YourDataModel shareInstance] setLike:false]; 

更新:

如果你想更新你的故事的ViewController從詳細信息頁回來。您可以在其viewWillAppear()代表中更新'like status'。您可以查看Apple's official document的詳細信息。

+0

好主意,但不適合的模式。 OP需要一個包含一系列故事的模型,這些故事具有諸如標題,文本,likeCount等屬性。它還必須提供某種方式來確定該用戶喜歡該故事。這可能表明模型中有一個用戶對象。在這種情況下,更大的模型(一個用戶和一個storyList)將是一個單身人士。 – danh

+0

@danh是的,你是對的。是否是用戶狀態,模型應根據OP的實際情況進行設計;)。 – Nickolas

+0

@Nickolas可我嘗試節省JSON文件中的數據,因爲像計數所有thease都可以從我以.json文件中提取的,所以如果我可以寫數據傳回JSON文件就可以真正幫助? –