从方法内部访问IBOutlet UIImageView

时间:2012-03-16 20:45:42

标签: iphone c ios xcode iboutlet

所以,我在ViewController.h中为一堆UIImageView实例设置了一些IBOutlets:

@property (strong, nonatomic) IBOutlet UIImageView *whiteKey21;

在我的实现中,当我在viewDidLoad方法中时,我可以访问IBOutlet:

[whiteKey21 setImage:image]; //image is declared and instantiated in viewDidLoad

我的问题是,当我尝试在另一个方法中执行相同的代码时,即这个:

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
    UserEvent* event = (UserEvent *)inEventData;
    UInt32 size = event->length;
    UInt32 note = event->playedNote;
    UInt32 timestamp = event->tStamp;
    NSLog(@"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);

    //Want to change the IBOutlet here.
}

它无法识别我在标题中声明的UIImages或UIImageViews。我觉得这是因为这个函数是基于C的,因为该函数必须与MusicPlayer API交互。但我不知道如何解决它?我尝试制作一个Obj-C方法,然后我可以在noteUserCallback函数中调用,但它也没有识别出:(

非常感谢任何帮助。谢谢:))

修改

以下是PracticeViewController.h和PracticeViewController.m

的完整代码

我想要做的是从noteUserCallback函数内部设置UIImageViews(whiteKey21等)显示的图像。只要在MIDI的回放中传递用户事件,该函数就会传递到某些数据(UserEvent)中。这允许我主动跟踪正在播放的音符。我需要做的是根据用户事件中的“注释”数据做出决定,UIImageView将图像加载到该数据中。但是,出于某种原因,我无法从函数内部访问UIImageViews或UIImages。我在其他方法中访问它们没有问题。

PracticeViewController.h

#import <UIKit/UIKit.h>
#import "Lesson.h"
#import "Note.h"
#import <AudioToolbox/AudioToolbox.h>

@interface PracticeViewController : UIViewController

@property (strong, nonatomic) Lesson *selectedLesson;
@property (strong, nonatomic) IBOutlet UINavigationItem *practiceWindowTitle;
@property MusicPlayer player;

//Outlets for White Keys
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey21;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey23;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey24;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey26;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey28;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey29;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey31;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey33;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey35;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey36;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey38;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey40;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey41;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey43;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey45;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey47;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey48;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey50;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey52;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey53;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey55;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey57;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey59;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey60;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey62;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey64;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey65;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey67;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey69;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey71;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey72;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey74;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey76;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey77;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey79;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey81;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey83;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey84;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey86;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey88;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey89;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey91;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey93;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey95;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey96;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey98;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey100;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey101;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey103;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey105;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey107;
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey108;

//Outlets for Black Keys
@property (strong, nonatomic) IBOutlet UIImageView *blackKey22;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey25;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey27;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey30;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey32;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey34;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey37;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey39;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey42;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey44;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey46;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey49;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey51;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey54;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey56;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey58;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey61;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey63;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey66;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey68;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey70;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey73;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey75;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey78;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey80;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey82;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey85;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey87;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey90;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey92;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey94;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey97;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey99;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey102;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey104;
@property (strong, nonatomic) IBOutlet UIImageView *blackKey106;

// Key Highlight Images
@property (strong, nonatomic) UIImage *highlightA;
@property (strong, nonatomic) UIImage *highlightB;
@property (strong, nonatomic) UIImage *highlightC;
@property (strong, nonatomic) UIImage *highlightD;
@property (strong, nonatomic) UIImage *highlightE;
@property (strong, nonatomic) UIImage *highlightF;
@property (strong, nonatomic) UIImage *highlightG;
@property (strong, nonatomic) UIImage *highlightH;

- (IBAction)practiceLesson:(id)sender;

@end

PracticeViewController.m

#import "PracticeViewController.h"

@interface PracticeViewController ()

@end

@implementation PracticeViewController
@synthesize blackKey22;
@synthesize blackKey25;
@synthesize blackKey27;
@synthesize blackKey30;
@synthesize blackKey32;
@synthesize blackKey34;
@synthesize blackKey37;
@synthesize blackKey39;
@synthesize blackKey42;
@synthesize blackKey44;
@synthesize blackKey46;
@synthesize blackKey49;
@synthesize blackKey51;
@synthesize blackKey54;
@synthesize blackKey56;
@synthesize blackKey58;
@synthesize blackKey61;
@synthesize blackKey63;
@synthesize blackKey66;
@synthesize blackKey68;
@synthesize blackKey70;
@synthesize blackKey73;
@synthesize blackKey75;
@synthesize blackKey78;
@synthesize blackKey80;
@synthesize blackKey82;
@synthesize blackKey85;
@synthesize blackKey87;
@synthesize blackKey90;
@synthesize blackKey92;
@synthesize blackKey94;
@synthesize blackKey97;
@synthesize blackKey99;
@synthesize blackKey102;
@synthesize blackKey104;
@synthesize blackKey106;
@synthesize whiteKey21;
@synthesize whiteKey23;
@synthesize whiteKey24;
@synthesize whiteKey26;
@synthesize whiteKey28;
@synthesize whiteKey29;
@synthesize whiteKey31;
@synthesize whiteKey33;
@synthesize whiteKey35;
@synthesize whiteKey36;
@synthesize whiteKey38;
@synthesize whiteKey40;
@synthesize whiteKey41;
@synthesize whiteKey43;
@synthesize whiteKey45;
@synthesize whiteKey47;
@synthesize whiteKey48;
@synthesize whiteKey50;
@synthesize whiteKey52;
@synthesize whiteKey53;
@synthesize whiteKey55;
@synthesize whiteKey57;
@synthesize whiteKey59;
@synthesize whiteKey60;
@synthesize whiteKey62;
@synthesize whiteKey64;
@synthesize whiteKey65;
@synthesize whiteKey67;
@synthesize whiteKey69;
@synthesize whiteKey71;
@synthesize whiteKey72;
@synthesize whiteKey74;
@synthesize whiteKey76;
@synthesize whiteKey77;
@synthesize whiteKey79;
@synthesize whiteKey81;
@synthesize whiteKey83;
@synthesize whiteKey84;
@synthesize whiteKey86;
@synthesize whiteKey88;
@synthesize whiteKey89;
@synthesize whiteKey91;
@synthesize whiteKey93;
@synthesize whiteKey95;
@synthesize whiteKey96;
@synthesize whiteKey98;
@synthesize whiteKey100;
@synthesize whiteKey101;
@synthesize whiteKey103;
@synthesize whiteKey105;
@synthesize whiteKey107;
@synthesize whiteKey108;

@synthesize selectedLesson, practiceWindowTitle, player, highlightA, highlightB, highlightC, highlightD, highlightE, highlightF, highlightG, highlightH;

// Implement the UserEvent structure.

typedef struct UserEvent {
    UInt32 length;
    UInt32 typeID;
    UInt32 trackID;
    MusicTimeStamp tStamp;
    MusicTimeStamp dur;
    int playedNote;
} UserEvent;

// Implement the UserCallback function.

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{
    PracticeViewController *pvc = (__bridge PracticeViewController *)inClientData;

    UserEvent* event = (UserEvent *)inEventData;
    UInt32 size = event->length;
    UInt32 note = event->playedNote;
    UInt32 timestamp = event->tStamp;
    NSLog(@"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);

    switch (note) {
        case 60:
            [pvc.whiteKey21 setImage:pvc.highlightA];
            break;

        default:
            break;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.practiceWindowTitle.title = selectedLesson.titleAndSubtitle;

    // Load in the images for the glow.
    highlightA = [UIImage imageNamed:@"glow_whiteKeysA.png"];
    highlightB = [UIImage imageNamed:@"glow_whiteKeysB.png"];
    highlightC = [UIImage imageNamed:@"glow_whiteKeysC.png"];
    highlightD = [UIImage imageNamed:@"glow_whiteKeysD.png"];
    highlightE = [UIImage imageNamed:@"glow_whiteKeysE.png"];
    highlightF = [UIImage imageNamed:@"glow_whiteKeysF.png"];
    highlightG = [UIImage imageNamed:@"glow_blackKey.png"];
    highlightH = [UIImage imageNamed:@"glow_whiteKeysH.png"];

    // Create player, sequence, left/right hand tracks, and iterator.

    NewMusicPlayer(&player);
    MusicSequence sequence;
    NewMusicSequence(&sequence);
    MusicTrack rightHand;
    MusicTrack leftHand;
    MusicEventIterator iterator;

    // Load in MIDI file.

    NSString *path = [[NSString alloc] init];
    path = [[NSBundle mainBundle] pathForResource:selectedLesson.midiFilename ofType:@"mid"];
    NSURL *url = [NSURL fileURLWithPath:path];
    MusicSequenceFileLoad(sequence, (__bridge CFURLRef)url, 0, kMusicSequenceLoadSMF_ChannelsToTracks);

    // Get the right and left hand tracks from the sequence.

    int rightHandIndex = 0;
    //int leftHandIndex = 1;

    MusicSequenceGetIndTrack(sequence, rightHandIndex, &rightHand); //Get right hand.
    //MusicSequenceGetIndTrack(sequence, leftHandIndex, leftHand); //Get left hand.

    //Iterate through the right hand track and add user events.

    Boolean hasNextEvent = false;
    Boolean hasEvent = false;

    NewMusicEventIterator(rightHand, &iterator);
    MusicEventIteratorHasCurrentEvent(iterator, &hasEvent);
    MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);

    while (hasNextEvent == true) {
        MusicTimeStamp timestamp = 0;
        MusicEventType eventType = 0;
        const void *eventData = NULL;
        int note;
        MusicTimeStamp duration;

        MusicEventIteratorGetEventInfo(iterator, &timestamp, &eventType, &eventData, NULL);

        if (eventType == kMusicEventType_MIDINoteMessage) {
            MIDINoteMessage *noteMessage = (MIDINoteMessage *)eventData;
            note = noteMessage->note;
            duration = noteMessage->duration;
            UserEvent event;

            // Hacky way of doing this...Change one we get the UserEvent class figured out.

            event.length = 0;
            event.length = sizeof(UserEvent);

            event.playedNote = note;

            event.tStamp = timestamp;

            MusicEventUserData* data = (MusicEventUserData *)&event;
            MusicTrackNewUserEvent(rightHand, timestamp, data);
        }

        MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);
        MusicEventIteratorNextEvent(iterator);
    }

    MusicSequenceSetUserCallback(sequence, noteUserCallback, (__bridge void*)self);

    MusicPlayerSetSequence(player, sequence);
    MusicPlayerPreroll(player);
}

- (void)viewDidUnload
{
    [self setPracticeWindowTitle:nil];
    [self setWhiteKey21:nil];
    [self setWhiteKey23:nil];
    [self setWhiteKey24:nil];
    [self setWhiteKey26:nil];
    [self setWhiteKey28:nil];
    [self setWhiteKey29:nil];
    [self setWhiteKey31:nil];
    [self setWhiteKey33:nil];
    [self setWhiteKey35:nil];
    [self setWhiteKey36:nil];
    [self setWhiteKey38:nil];
    [self setWhiteKey40:nil];
    [self setWhiteKey41:nil];
    [self setWhiteKey43:nil];
    [self setWhiteKey45:nil];
    [self setWhiteKey47:nil];
    [self setWhiteKey48:nil];
    [self setWhiteKey50:nil];
    [self setWhiteKey52:nil];
    [self setWhiteKey53:nil];
    [self setWhiteKey55:nil];
    [self setWhiteKey57:nil];
    [self setWhiteKey59:nil];
    [self setWhiteKey60:nil];
    [self setWhiteKey62:nil];
    [self setWhiteKey64:nil];
    [self setWhiteKey65:nil];
    [self setWhiteKey67:nil];
    [self setWhiteKey69:nil];
    [self setWhiteKey71:nil];
    [self setWhiteKey72:nil];
    [self setWhiteKey74:nil];
    [self setWhiteKey76:nil];
    [self setWhiteKey77:nil];
    [self setWhiteKey79:nil];
    [self setWhiteKey81:nil];
    [self setWhiteKey83:nil];
    [self setWhiteKey84:nil];
    [self setWhiteKey86:nil];
    [self setWhiteKey88:nil];
    [self setWhiteKey89:nil];
    [self setWhiteKey91:nil];
    [self setWhiteKey93:nil];
    [self setWhiteKey95:nil];
    [self setWhiteKey96:nil];
    [self setWhiteKey98:nil];
    [self setWhiteKey100:nil];
    [self setWhiteKey100:nil];
    [self setWhiteKey101:nil];
    [self setWhiteKey103:nil];
    [self setWhiteKey105:nil];
    [self setWhiteKey107:nil];
    [self setWhiteKey108:nil];
    [self setBlackKey22:nil];
    [self setBlackKey25:nil];
    [self setBlackKey27:nil];
    [self setBlackKey30:nil];
    [self setBlackKey32:nil];
    [self setBlackKey34:nil];
    [self setBlackKey37:nil];
    [self setBlackKey39:nil];
    [self setBlackKey42:nil];
    [self setBlackKey44:nil];
    [self setBlackKey46:nil];
    [self setBlackKey49:nil];
    [self setBlackKey51:nil];
    [self setBlackKey54:nil];
    [self setBlackKey56:nil];
    [self setBlackKey58:nil];
    [self setBlackKey61:nil];
    [self setBlackKey63:nil];
    [self setBlackKey66:nil];
    [self setBlackKey68:nil];
    [self setBlackKey70:nil];
    [self setBlackKey73:nil];
    [self setBlackKey75:nil];
    [self setBlackKey78:nil];
    [self setBlackKey80:nil];
    [self setBlackKey82:nil];
    [self setBlackKey85:nil];
    [self setBlackKey87:nil];
    [self setBlackKey90:nil];
    [self setBlackKey92:nil];
    [self setBlackKey94:nil];
    [self setBlackKey97:nil];
    [self setBlackKey99:nil];
    [self setBlackKey102:nil];
    [self setBlackKey104:nil];
    [self setBlackKey106:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)practiceLesson:(id)sender {
    MusicPlayerStart(player);
}
@end

修改

与MusicSequenceSetUserCallback相关的代码。这是在MusicPlayer API(AudioToolbox / MusicPlayer.h)中声明的:

/*!
    @typedef MusicSequenceUserCallback
@discussion See MusicSequenceSetUserCallback
*/

typedef void (*MusicSequenceUserCallback)(  void                        *inClientData,
                                            MusicSequence               inSequence,
                                            MusicTrack                  inTrack,
                                            MusicTimeStamp              inEventTime,
                                            const MusicEventUserData    *inEventData,
                                            MusicTimeStamp              inStartSliceBeat,
                                            MusicTimeStamp              inEndSliceBeat);


/*!
    @function   MusicSequenceSetUserCallback
    @abstract   Establish a user callback for a sequence
    @discussion This call is used to register (or remove if inCallback is NULL) a callback
                that the MusicSequence will call for ANY UserEvents that are added to any of the
                tracks of the sequence.

                If there is a callback registered, then UserEvents will be chased when
                MusicPlayerSetTime is called. In that case the inStartSliceBeat and inEndSliceBeat
                will both be the same value and will be the beat that the player is chasing too.

                In normal cases, where the sequence data is being scheduled for playback, the
                following will apply:
                    inStartSliceBeat <= inEventTime < inEndSliceBeat

                The only exception to this is if the track that owns the MusicEvent is looping.
                In this case the start beat will still be less than the end beat (so your callback
                can still determine that it is playing, and what beats are currently being scheduled),
                however, the inEventTime will be the original time-stamped time of the user event. 
    @param      inSequence      the sequence
    @param      inCallback      the callback
    @param      inClientData    client (user supplied) data provided back to the callback when it is called by the sequence
*/
extern OSStatus
MusicSequenceSetUserCallback(   MusicSequence               inSequence,
                                MusicSequenceUserCallback   inCallback,
                                void*                       inClientData)       __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_5_0);

1 个答案:

答案 0 :(得分:0)

在所有CoreAudio回调中,你有一个“用户数据”参数,它可能在你的回调void *inClientData中,它允许传递给用户想要传递的回调任何对象。 在您注册回调的情况下,例如当您创建音频队列输入时,您将参数的长列表传递回调的名称,但您也可以使用inClientData指针传递您希望将发送到回调的用户数据结构的地址之前提到的。在您的情况下,您可以传递的是包含UIImageViews的类实例的地址(例如,如果您的类实例是“myObj”,那么您将传递&myObj)。

所以在回调中你会以这种方式取消引用它:


MyObject *myObject = (MyObject *)inClientData;

现在,在您的代码中,您可以通过以下方式设置MusicSequenceCallback:


MusicSequenceSetUserCallback(sequence, noteUserCallback, NULL);

第三个参数是void *inClientData,这个数据作为回调的第一个参数传递。因此,您必须传递包含您需要使用的所有UIImageView的对象,而不是NULL;


MusicSequenceSetUserCallback(sequence, noteUserCallback, myObject);

然后你就可以在这个obj实例上使用任何obj-c方法。指向此对象的指针将传递给回调,然后您只需要在回调中取消引用它。