2011年9月20日 星期二

Lala's Program Note 實作記錄: 21. 為文章加上我的最愛

since: 2011/09/20
update: 2012/02/12


A. 開啟 ArticleViewController.h 檔案, 修改如下:

#import <UIKit/UIKit.h>

@interface ArticleViewController : UIViewController {
    //@add
    IBOutlet UILabel *titleLabel; // article 標題
    IBOutlet UILabel *subTitleLabel; // article 副標題
    IBOutlet UITextView *contentText; // article 內容
    NSManagedObject *article; // represents the current article
   
    //@add
    NSManagedObjectContext *managedObjectContext;
    BOOL favorite;
    IBOutlet UIImageView *myFavorite;   // My Favorite 我的最愛 UI
    NSString *favoriteFileName;           // My Favorite images file
    NSString *notFavoriteFileName;       // not My Favorite images file
    CGPoint touchPoint;
}

//@add
@property (nonatomic, retain) IBOutlet UILabel *titleLabel;
@property (nonatomic, retain) IBOutlet UILabel *subTitleLabel;
@property (nonatomic, retain) IBOutlet UITextView *contentText;
@property (nonatomic, retain) NSManagedObject *article;

//@add
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) IBOutlet UIImageView *myFavorite;
//@add:切換 Favorite 圖檔 (update: 2012/02/12)
- (IBAction)toggleFavoriteImage:(BOOL)aFavorite;
- (void)toggleFavoriteImage:(BOOL)aFavorite;

@end

----------------------------------------------------------------------------------------------------

B. 開啟 ArticleViewController.m 檔案, 修改如下:

#import "ArticleViewController.h"

@implementation ArticleViewController

//@add
@synthesize titleLabel, subTitleLabel, contentText, article;
//@add
@synthesize managedObjectContext, myFavorite;

//@add
- (void)dealloc {
    [managedObjectContext release];
    [myFavorite release];

    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    //@add
    self.titleLabel = nil;
    self.subTitleLabel = nil;
    self.contentText = nil;
    //@add
    self.myFavorite = nil;
}


//@add
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    touchPoint = [myTouch locationInView:self.view];
   
    if ((touchPoint.x >= myFavorite.frame.origin.x) &&
        (touchPoint.x <= myFavorite.frame.origin.x + 50) &&
        (touchPoint.y >= myFavorite.frame.origin.y) &&
        (touchPoint.y <= myFavorite.frame.origin.y + 50)) {
       
        if (article != nil) {
             //Core Data "does not have" a Boolean type (it does, but it is an NSNumber).
             [article setValue:[NSNumber numberWithBool:!favorite] forKey:@"favorite"];
           
            NSError *error = nil;
            if (![managedObjectContext save:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
            else {
                favorite = !favorite;
                [self toggleFavoriteImage:favorite];
            }
        }
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //@add
    if (article != nil) {
        titleLabel.text = [article valueForKey:@"title"];
        subTitleLabel.text = [article valueForKey:@"subTitle"];
        contentText.text = [article valueForKey:@"content"];
       
        //@add
        favoriteFileName = @"iStar-yes-50.png";
        notFavoriteFileName = @"iStar-no-50.png";
       
        //Core Data "does not have" a Boolean type (it does, but it is an NSNumber).
        favorite = [[article valueForKey:@"favorite"] boolValue];
       
        [self toggleFavoriteImage:favorite];
    }
    else {
    }
}


//@add:切換 Favorite 圖檔 (update: 2012/02/12)
- (IBAction)toggleFavoriteImage:(BOOL)aFavorite {
- (void)toggleFavoriteImage:(BOOL)aFavorite {
    if (aFavorite) {
        myFavorite.image = [UIImage imageNamed:favoriteFileName];
    }
    else {
        myFavorite.image = [UIImage imageNamed:notFavoriteFileName];
    }
}

----------------------------------------------------------------------------------------------------

C. 開啟 ArticleListViewController.m 檔案, 修改如下:

//@add
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
   
    ArticleViewController *articleViewController = [[ArticleViewController alloc] init];
   
    NSManagedObject *article = [[self fetchedResultsController] objectAtIndexPath:indexPath];
   
    //articleViewController.title = [NSString stringWithFormat:@"%@",[article valueForKey:@"title"]];
    //@update
    articleViewController.article = article;
   
    //@update
    articleViewController.managedObjectContext = self.managedObjectContext;
   
    [self.navigationController pushViewController:articleViewController animated:YES];
   
    [articleViewController release];
}

----------------------------------------------------------------------------------------------------

D. 開啟 SearchViewController.m 檔案, 修改如下:

//@add
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleViewController *articleViewController = [[ArticleViewController alloc] init];
   
    NSManagedObject *article = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    articleViewController.article = article;
   
    //@relationship
    articleViewController.title = [[article valueForKey:@"notebook"] valueForKey:@"name"];
   
    //@add
    articleViewController.managedObjectContext = self.managedObjectContext;
   
    [self.navigationController pushViewController:articleViewController animated:YES];
   
    [articleViewController release];
}

----------------------------------------------------------------------------------------------------

E. 開啟 ArticleViewController.xib 檔案, 修改如下:
   1. 新增一個 UIImageView, 置於右上方大小為 50 x 50
   2. 將 File's Owner 裡的 myFavorite 連結到此 UIImageView.
  
----------------------------------------------------------------------------------------------------

F. 結果圖:


2 則留言:

  1. - (IBAction)toggleFavoriteImage:(BOOL)aFavorite; 請問這ibAction聯到在xib的那裡, 是一個按紐嗎? 在範例圖示好像沒看到?

    回覆刪除
  2. 你好, 謝謝你的提醒, 這個方法應該可以不用使用 IBAction, 直接將方法名稱改成: - (void)toggleFavoriteImage:(BOOL)aFavorite; 其它說明如下:

    1. 定義: IBOutlet UIImageView *myFavorite; 用來放我的最愛的圖.
    2. 準備二張圖, 在此為: iStar-yes-50.png 與 iStar-no-50.png (紅星與灰星)
    3. 利用: - (void)touchesBegan:withEvent: 方法, 來偵測當使用者碰觸到
    紅/灰星時作 "我的最愛" 圖形的切換.

    回覆刪除

注意:只有此網誌的成員可以留言。