按下不同按钮时显示两个不同的阵列

时间:2012-05-11 13:42:16

标签: iphone ios xcode arrays uiviewcontroller

我有一个名为ForthViewControler的UIViewController,带有两个按钮属性:
.h文件:

#import <UIKit/UIKit.h>

@interface ForthViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIButton* imgBtn;
@property (nonatomic, strong) IBOutlet UIButton* imgBtn2;

@end

在.m文件中,我有那些连接到Interface Builder中按钮的方法:

#import "SDWebImageRootViewController.h"

@implementation ForthViewController

@synthesize imgBtn, imgBtn2;

-(IBAction)menueBtnAction:(id)sender
{    
    SDWebImageRootViewController* tivc1 = [[SDWebImageRootViewController alloc] initWithNibName:@"SDWebImageRootViewController" bundle:nil];
    [self.navigationController pushViewController:tivc1 animated:NO];
}

-(IBAction)menueBtnActionAfter:(id)sender
{    
    SDWebImageRootViewController* tivc1 = [[SDWebImageRootViewController alloc] initWithNibName:@"SDWebImageRootViewController" bundle:nil];
    [self.navigationController pushViewController:tivc1 animated:NO];   
}

这些方法导致SDWebImageRootViewController 这就是我在SDWebImageRootViewController中的含义:
.h文件:

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

@class SDWebImageDataSource;

@interface SDWebImageRootViewController : KTThumbsViewController 
{
@private
   SDWebImageDataSource *images_;
   UIWindow *window_;
}

@end

.m文件:

#import "SDWebImageRootViewController.h"
#import "SDWebImageDataSource.h"

@interface SDWebImageRootViewController ()

@end

@implementation SDWebImageRootViewController
}

- (void)viewDidLoad 
{
   [super viewDidLoad];

   self.title = @"SDWebImage Sample";

   images_ = [[SDWebImageDataSource alloc] init];
   [self setDataSource:images_];
}

最后,在SDWebImageDataSource文件中我有这个:
.h文件:

#import <Foundation/Foundation.h>
#import "KTPhotoBrowserDataSource.h"

@interface SDWebImageDataSource : NSObject <KTPhotoBrowserDataSource>
{
   NSMutableArray *images_;
}

@end

.m文件:

#import "SDWebImageDataSource.h"
#import "KTPhotoView+SDWebImage.h"
#import "KTThumbView+SDWebImage.h"
#import "ForthViewController.h"

#define FULL_SIZE_INDEX 0
#define THUMBNAIL_INDEX 1

@implementation SDWebImageDataSource

- (id)init
{
    self = [super init];
    if (self)
{
    // Create a 2-dimensional array. First element of
    // the sub-array is the full size image URL and 
    // the second element is the thumbnail URL.

    ForthViewController* tivc1 = [[ForthViewController alloc] initWithNibName:@"ForthViewController" bundle:nil];

    if (tivc1.imgBtn.selected == YES)
    {

        images_ = [[NSMutableArray alloc] initWithObjects:


                   [NSArray arrayWithObjects:@"http://farm6.static.flickr.com/5181/5554192246_e7cf81fb00_z.jpg", @"http://farm6.static.flickr.com/5181/5554192246_e7cf81fb00_s.jpg", nil],
                   [NSArray arrayWithObjects:@"http://farm6.static.flickr.com/5260/5554955879_01bfab9aeb_z.jpg", @"http://farm6.static.flickr.com/5260/5554955879_01bfab9aeb_s.jpg", nil],
                   [NSArray arrayWithObjects:@"http://farm6.static.flickr.com/5051/5556628277_f883fa1078_z.jpg", @"http://farm6.static.flickr.com/5051/5556628277_f883fa1078_s.jpg", nil], nil];
    }

    else
    {
        if (tivc1.imgBtn2.selected == YES)
        {
        images_ = [[NSMutableArray alloc] initWithObjects:
                   [NSArray arrayWithObjects:@"http://dl.dropbox.com/u/6089026/icon2.png", @"http://dl.dropbox.com/u/6089026/icon2.png", nil],
                   [NSArray arrayWithObjects:@"http://farm4.static.flickr.com/3427/3192205971_0f494a3da2_o.jpg", @"http://farm4.static.flickr.com/3427/3192205971_b7b18558db_s.jpg", nil],
                   [NSArray arrayWithObjects:@"http://farm2.static.flickr.com/1316/4722532733_6b73d00787_z.jpg", @"http://farm2.static.flickr.com/1316/4722532733_6b73d00787_s.jpg", nil],
                   [NSArray arrayWithObjects:@"http://farm2.static.flickr.com/1200/591574815_8a4a732d00_o.jpg", @"http://farm2.static.flickr.com/1200/591574815_29db79a63a_s.jpg", nil],
                   [NSArray arrayWithObjects:@"http://dl.dropbox.com/u/6089026/mika4.png", @"http://dl.dropbox.com/u/6089026/mika4.png", nil], nil];
            }
        }
    }
            return self;

}

#pragma mark -
#pragma mark KTPhotoBrowserDataSource

- (NSInteger)numberOfPhotos
{
NSInteger count = [images_ count];
return count;
}

- (void)imageAtIndex:(NSInteger)index photoView:(KTPhotoView *)photoView {
    NSArray *imageUrls = [images_ objectAtIndex:index];
    NSString *url = [imageUrls objectAtIndex:FULL_SIZE_INDEX];
    [photoView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"photoDefault.png"]];
}

- (void)thumbImageAtIndex:(NSInteger)index thumbView:(KTThumbView *)thumbView {
    NSArray *imageUrls = [images_ objectAtIndex:index];
    NSString *url = [imageUrls objectAtIndex:THUMBNAIL_INDEX];
    [thumbView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"photoDefault.png"]];
}

@end

如您所见,我试图调用ForthViewController并使用两个按钮作为“f”的声明。
问题是每次我按下两个按钮中的一个(在我的ForthViewController nib文件中)它会导致相同的数组... 我很高兴知道我做错了什么? 对不起,很长的帖子,我想提供最详细的信息:) 感谢。

2 个答案:

答案 0 :(得分:0)

你应该设置选择你的按钮。在你的IBAction方法

   [tivc1.imgBtn setSelected:YES];

   [tivc1.imgBtn2 setSelected:YES];

在他们的方法中。然后它将正常工作。

答案 1 :(得分:0)

你没有正确使用

if (tivc1.imgBtn2.selected == YES)
        {.....

一件事就是将按钮设置为

imgBtn2.selected = YES 

但如果您在其他地方分配此类的对象,则可能会导致错误

第二个,也就是说,它不会给你正确的结果,因为它们现在不再被选中,并且总是只访问第一个条件部分,在数据源中使用一些属性并为按钮1和2设置值为1对于buttone 2,然后检查该属性而不是按钮