2012-08-06 2 views
0

즐겨 찾기 목록에 개체를 추가하기 위해 UIbutton (스토리 보드에 있음)을 만들었지 만 구성하는 데 문제가 있습니다. 이것은 객체를 즐겨 찾기에 추가하지만, 즐겨 찾기를 해제하는 방법은 무엇입니까?강조 표시가있는 UIButton 구성

저는 잠시 동안 검색을 해봤습니다. 내 생각은 manageHighlightAndFave에서 if 문을 다음과 같이 만드는 것입니다. if favButton state = highlighted, 즐겨 찾기에서 제거하고 강조 표시를 제거하십시오. 그렇지 않으면 즐겨 찾기에 추가하고 강조 표시를 추가하십시오. 이게 좋은가요, 아니면 제가 시도하고있는 일을하는 걸 선호하는 방법입니까? 내가 프로그래밍을 처음 접했을 때 나는 그 예를 좋아할 것이다.

-(IBAction)favoriteButtonPressed:(id)sender 
{ 
    [self performSelector:@selector(manageHighlightAndFave:) withObject:sender afterDelay:0]; 
} 

- (void)manageHighlightAndFave:(UIButton*)favButton { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected" 
                 object:selectedObject]; 
    [favButton setHighlighted:YES]; 
} 

추신. 스토리 보드에서 '터치 다운'과 연결됩니다.

답변

1

맞춤 버튼을 만드는 것이 좋습니다. 다음 참조 코드.

먼저 다음 버튼을 만드십시오. 파일 - 뉴 - 파일 - 코코아 터치 오브젝티브 C 클래스 enter image description here enter image description here FavoriteButton.h

#import <UIKit/UIKit.h> 

@interface FavoriteButton : UIButton 

@property (nonatomic, assign) BOOL isFavorite; 
@end 

FavoriteButton.m

#import "FavoriteButton.h" 

@implements FavoriteButton : UIButton 

@synthesize isFavorite; 
... 
@end 

괜찮다 condory에서 Storyboard에서 FavoriteButton을 연결합니다. 다음 이미지를 참조하십시오. 스토리 보드의 오른쪽 패널에 있습니다. "isFavorite"유형의 개체에없는 속성 : 전에, 당신의 원래있는 UIButton

enter image description here

YourViewController.h

#import <UIKit/UIKit.h> 
#import "FavoriteButton.h" 

@interface YourViewController : UIViewController 
@property (retain, nonatomic) IBOutlet FavoriteButton *favoriteButton; 
@end 

@implements YourViewController : UIViewController 
@synthesize favoriteButton; 


-(void) viewDidLoad 
{ 
    self.favoriteButton = [[FavoriteButton alloc] initWithFrame:...]]; 
    //favoriteButton.isFavorite = NO; (already set in storyboard) 
    ... 
} 

-(IBAction)favoriteButtonPressed:(id)sender 
{ 
    [self performSelector:@selector(manageHighlightAndFave:) withObject:sender afterDelay:0]; 
} 

- (void)manageHighlightAndFave:(FavoriteButton *)favButton { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected" 
                 object:selectedObject]; 

    //true-false(YES-NO) Toggled. but isFavorite property is Must be initialized to false. 
    favButton.isFavorite = !favButton.isFavorite; 

    if(favButton.isFavorite) 
    { 
     favButton.imageView.image = [UIImage imageNamed:@"your_star_image"]; 
     [favButton setHighlighted:YES]; 
    } 
    else 
    { 
     favButton.imageView.image = nil; 
     [favButton setHighlighted:NO]; 
    } 
} 
+0

내가 이것을 시도,하지만 난 4 오류가 발생할 수 클릭 UIButton x 2 및 "UIButton에 대해 표시되지 않는 @interface는 선택기"setHighlighted "x 2를 선언합니다. fav를 추가하는 알림에 'if'블록을 배치하고 fav를 제거하라는 알림을 배치해야합니다 '다른'블록에? 아니면 내가 이것을 오해하고 있습니까? – ingenspor

+1

내가 편집 한 게시물. 체크 아웃하십시오. –

+0

매우 감사합니다. – ingenspor

관련 문제