Clicking on Like all the view should show like objective C

こ雲淡風輕ζ 提交于 2019-12-23 01:11:52

问题


When I select Throwback -> Description occurs if I click on like button the like button changes to unlike and " like count=1099" increases. If I press back I want this unlike to be displayed next to Throwback let say in a label and again select Throwback button should display unlike and like Count should be 1100. please Help how can i achieve this ?

//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;
    }
}

回答1:


You can encapsulate the 'likes' number and status into a data model.
Both your controllers can access and modify the data model. In this case, data model can be a singleton, so that you can acquire it in your controllers.
May be you need sync data to server or persistent data to local storage later, data model can encapsulate all this service.

Here is some code sample

// 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];

Update:

If you want to update your story viewcontroller back from detail page. You can update 'like status' in its viewWillAppear() delegate. You can check details on Apple's official document.



来源:https://stackoverflow.com/questions/40674494/clicking-on-like-all-the-view-should-show-like-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!