2015-04-03 82 views
1

我在我的ViewController中有一個名爲chordName的NSString變量,它是從單獨的TableViewController設置的值。 我想再次傳遞這個變量,以便在ViewController中的自定義視圖中使用。如何從自定義UIView中的ViewController傳遞數據

我將如何傳遞變量從這裏(viewcontroller.m)

#import "P15ViewController.h" 
#import "P15AppDelegate.h" 
@interface P15ViewController() 

@end 

@implementation P15ViewController 

@synthesize intLabel; 
@synthesize chordNameLabel; 
@synthesize chordName; 
- (void)viewDidLoad 
{ 

    self.guitarStringView.delegate = self; 
    chordNameLabel.text = chordName; 
    [super viewDidLoad]; 

} 

-(void)guitarStringsView:(GuitarStringsView*)view stringPlucked:(int)index withVelocity:(float)velocity; 
{  
    P15AppDelegate* app = [[UIApplication sharedApplication] delegate]; 
    [app play:index velocity:velocity]; 
} 

@end 

進入了UIView類drawRect方法中使用。 我還是新手,所以讓我知道你是否需要更多的代碼。

感謝

//編輯 - 這裏是自定義視圖類:

.h 

#import <UIKit/UIKit.h> 

@class GuitarStringsView; 
@protocol GuitarStringsViewDelegate <NSObject> 

-(void)guitarStringsView:(GuitarStringsView*)view stringPlucked:(int)index withVelocity:(float)velocity; 
@end 

@interface GuitarStringsView : UIView 

@property (nonatomic) int stringCount; 

@property (weak, nonatomic) id <GuitarStringsViewDelegate> delegate; 

@end 

.M

@interface GuitarStringsView() 
@property (nonatomic, strong) NSMutableArray* touches; 
@property (nonatomic, strong) NSMutableArray* timestamps; 
@end 

@implementation GuitarStringsView 

@synthesize stringCount = _stringCount; 


- (id)initWithFrame:(CGRect)frame 
{ 
    if ([super initWithFrame:frame]) 
    { 
     [self commonInit]; 
    } 

    return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if ([super initWithCoder:aDecoder]) 
    { 
     [self commonInit]; 
    } 

    return self; 
} 

- (void)commonInit 
{ 
    // initialise string count and the arrays to log the touches 
    self.stringCount = 1; 
    self.touches = [[NSMutableArray alloc] initWithCapacity:2]; 
    self.timestamps = [[NSMutableArray alloc] initWithCapacity:2]; 
} 

// stringCount getter 
-(int)stringCount 
{ 
    return _stringCount; 
} 

// stringCount setter 
-(void)setStringCount:(int)stringCount 
{ 
    // clip to >= 1 
    if (stringCount < 1) 
     stringCount = 1; 

    // if changed 
    if (stringCount != _stringCount) 
    { 
     // update 
     _stringCount = stringCount; 

     // tell the system it needs to redraw this view 
     [self setNeedsDisplay]; 
    } 
} 

// drawing 
- (void)drawRect:(CGRect)rect 
{ 
    // get the "context" which is the place to which we will draw 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    // save the state (colours, pen width etc) so we can restore it when we're done 
    CGContextSaveGState(currentContext); 

    // set up fill and stroke colours 
    CGContextSetRGBFillColor(currentContext, 0.5, 0.0, 0.0, 1.0); // dark red 
    CGContextSetRGBStrokeColor(currentContext, 0.0, 0.0, 0.0, 1.0); // black 

    // set the stroke pen width 
    CGContextSetLineWidth(currentContext, 1); 

    // fill whole view with fill colour 
    CGContextFillRect(currentContext, rect); 

    // calculate geometry, divided screen into N sections where N is stringCount + 1 
    const int divisions = self.stringCount + 1; 
    const CGFloat divisionWidth = self.bounds.size.width/divisions; 

    // start at the first string 
    CGFloat stringX = divisionWidth; 

    for (int i = 0; i < self.stringCount; ++i, stringX += divisionWidth) // move to next string position each time 
    { 
     // add a line to the path 
     CGContextMoveToPoint(currentContext, stringX, 0); 
     CGContextAddLineToPoint(currentContext, stringX, self.bounds.size.height); 
    } 

    // draw the path (the strings) 
    CGContextStrokePath(currentContext); 

    // restore the context 
    CGContextRestoreGState(currentContext); 

    //Draw frets 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    CGFloat components[] = {0.0, 0.0, 1.0, 1.0}; 
    CGColorRef color = CGColorCreate(colorspace, components); 
    CGContextSetStrokeColorWithColor(context, color); 
    for (int i = 1; i < 5; ++i) // move to next fret position each time 
    { 
     // add a line to the path 
     CGContextMoveToPoint(context, 1, (i * 100)); 
     CGContextAddLineToPoint(context, 300, (i * 100)); 
    } 

    CGContextStrokePath(context); 
    CGColorSpaceRelease(colorspace); 
    CGColorRelease(color); 


    //draw finger markers 
    CGColorRef red = [[UIColor redColor] CGColor]; 
    CGContextRef fingerContext = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(fingerContext, red); 
    CGContextFillEllipseInRect(context, CGRectMake(60, 140, 40, 40)); 

} 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
{ 
    // iterate over the touches 
    for (UITouch* touch in touches) 
    { 
     // find just the touches that have just begun (finger down) 
     if (touch.phase == UITouchPhaseBegan) 
     { 
      // add the touch to the touches array 
      [self.touches addObject:touch]; 

      // get the timestamp of the touch and add that as an NSNumber to the timestamps array 
      NSNumber* timestamp = [NSNumber numberWithDouble:touch.timestamp]; 
      [self.timestamps addObject:timestamp]; 
     } 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // calculate geometry, divided screen into N sections where N is stringCount + 1 
    const int divisions = self.stringCount + 1; 
    const CGFloat divisionWidth = self.bounds.size.width/divisions; 

    // iterate over the touches 
    for (UITouch* touch in touches) 
    { 
     // find just the touches that have just moved 
     if (touch.phase == UITouchPhaseMoved) 
     { 
      // compare the timestamps of the last time we saw this touch and now 
      // to get the duration since we last saw this touch 
      const int timestampIndex = [self.touches indexOfObject:touch]; 
      NSNumber* prevTimestampObject = self.timestamps[timestampIndex]; 

      const NSTimeInterval prevTimestamp = [prevTimestampObject doubleValue]; 
      const NSTimeInterval thisTimestamp = touch.timestamp; 
      const NSTimeInterval duration = thisTimestamp - prevTimestamp; 

      // get the previous and current x positions of this touch 
      const CGFloat hereX = [touch locationInView:self].x; 
      const CGFloat prevX = [touch previousLocationInView:self].x; 

      CGFloat stringX = divisionWidth; 

      // iterate over the strings 
      for (int i = 0; i < self.stringCount; ++i, stringX += divisionWidth) 
      { 
       // did the touch cross this string since the last time we saw this touch? 
       if (((prevX < stringX) && (hereX >= stringX)) || 
        ((prevX > stringX) && (hereX <= stringX))) 
       { 
        // calculate a velocity value based on the speed the finger was moving 
        const CGFloat distance = fabsf (hereX - prevX)/self.bounds.size.width; 
        const CGFloat velocity = (distance/duration)/self.stringCount; 

        // send the pluck message to our delegate, clipping velocity to <= 1 
        [self.delegate guitarStringsView:self 
             stringPlucked:i 
             withVelocity:velocity < 1 ? velocity : 1.f]; 
       } 
      } 

      // update the strored timestamp for this touch for next time 
      self.timestamps[timestampIndex] = [NSNumber numberWithDouble:thisTimestamp]; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // iterate over the touches 
    for (UITouch* touch in touches) 
    { 
     // just get touches that have ended or have been cancelled 
     if ((touch.phase == UITouchPhaseEnded) || 
      (touch.phase == UITouchPhaseCancelled)) 
     { 
      // get the index of this touch in our touches array 
      const int index = [self.touches indexOfObject:touch]; 

      // remove the touch from the touches array 
      [self.touches removeObjectAtIndex:index]; 

      // remove the associated timestamp 
      [self.timestamps removeObjectAtIndex:index]; 
     } 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // do same as touchesEnded: in this case 
    [self touchesEnded:touches withEvent:event]; 
} 


@end 
+0

請添加您的自定義視圖代碼。那麼我們可以幫助你更好。 – Jassi 2015-04-03 16:15:27

+0

將其添加到問題 – 2015-04-03 16:21:56

+0

我已添加答案。請檢查。 – Jassi 2015-04-03 16:29:59

回答

1

這裏有您需要在您的GuitarStringsView

#import <UIKit/UIKit.h> 
@class GuitarStringsView; 
@protocol GuitarStringsViewDelegate <NSObject> 
-(void)guitarStringsView:(GuitarStringsView*)view stringPlucked:(int)index withVelocity:(float)velocity; 
@end 

@interface GuitarStringsView : UIView 
@property (nonatomic) int stringCount; 
@property(nonatomic) NSString *chordName; 
@property (weak, nonatomic) id <GuitarStringsViewDelegate> delegate; 
@end 
進行更改

接下來,您可以在您的視圖控制器設置viewDidLoad:方法

self.guitarStringView.chordName = chordName; 
0

如果您自定義的UIView是UIViewController中所示之內,你應該有一個用名爲chordName的屬性爲您的UIView定製類。您的UIViewController應該訪問您的UIView並將其屬性設置爲新的chordName。 您的代碼將是這樣的

Declare CustomView *myCustomView in your header file 
myCustomView = [CustomView new]; 
[self.view addsubview:myCustomView] 

現在需要的時候,可以通過設定值 myCustomView.chordValue = chordValue

+0

我有一個自定義類爲該UIView,但我將如何訪問該視圖並設置其屬性? – 2015-04-03 16:08:17

+0

這是您的自定義視圖,它在哪裏添加?它是否添加到相同的UIViewController? – 2015-04-03 16:12:04

+0

是的,它與我想要傳遞的變量在同一個ViewController中 – 2015-04-03 16:13:55

相關問題