1. 程式人生 > >iOS -- delegate

iOS -- delegate

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    DelegateView *delegateV = [[DelegateView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
#warning 第五步: 指定代理人
    delegateV.delegate = self;
    [self.view addSubview:delegateV];
    
}

#warning 第六步: 實現代理方法
- (void)changeLocation:(UIView *)view
{
    if (view.frame.origin.y > self.view.frame.size.height - 100) {
        
        view.frame = CGRectMake(100, 100, 100, 100);
        
    }else
    {
         view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + 10, view.frame.size.width, view.frame.size.height);
    }
}
- (void)changeColor:(UIView *)view
{
    view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1];
}