程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Objective-C»ù´¡±Ê¼Ç£¨5£©Protocol

Objective-C»ù´¡±Ê¼Ç£¨5£©Protocol

編輯:關於C語言

Objective-C»ù´¡±Ê¼Ç£¨5£©Protocol


Protocol¼òµ¥À´Ëµ¾ÍÊÇһϵÁз½·¨µÄÁÐ±í£¬ÆäÖÐÉùÃ÷µÄ·½·¨¿ÉÒÔ±»ÈκÎÀàʵÏÖ¡£ÕâÖÐģʽһ°ã³ÆΪ´úÀí£¨delegation)ģʽ¡£

ÔÚIOSºÍOS X¿ª·¢ÖУ¬Apple²ÉÓÃÁË´óÁ¿µÄ´úÀíģʽÀ´ÊµÏÖMVCÖÐView(UI¿Ø¼þ£©ºÍController(¿ØÖÆÆ÷£©µÄ½âñî¡£

ÏÂÃæÎÒÃÇÏÈÀ´¿´Ò»ÏÂÎÒÃÇÊìϤµÄAndroidÖеİ´Å¥¼àÌý¹ý³Ì£¬È»ºóÔÙ¶Ô±ÈÀí½âdelegation¡£

Ê×ÏÈÎÒ½¨Á¢Ò»¸öºÜ¼òµ¥µÄAndroid¹¤³Ì£¬ÔÚLayoutÖзÅÖÃÒ»¸ö°´Å¥£¬ÈçÏ£º


	
package com.example.helloword;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.mybutton);
        button.setOnClickListener(new MyOnClickListener());
    }
    
    class MyOnClickListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			Toast.makeText(MainActivity.this, 
					"µã»÷ÁË°´Å¥", Toast.LENGTH_SHORT).show();
		}
    }
    
    class MyonLongClickListener implements OnLongClickListener{

		@Override
		public boolean onLongClick(View arg0) {
			Toast.makeText(MainActivity.this, 
					"³¤°´ÁË°´Å¥", Toast.LENGTH_SHORT).show();
			return false;
		}
    	
    }
}
OnClickListenerÊÇViewµÄÒ»¸öÄÚ²¿À࣬ÊÇView¶¨ÒåµÄÒ»¸ö½Ó¿Ú£¬ÎÒÃÇ´ò¿ªOnClickListenerÔ´ÂëÈçÏ£º

    /**
     * Interface definition for a callback to be invoked when a view is clicked.
     */
    public interface OnClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v);
    }
ÎÒÃÇÔÙÀ´¿´¿´setOnClickListener·½·¨

    public void setOnClickListener(OnClickListener l) {
        if (!isClickable()) {
            setClickable(true);
        }
        getListenerInfo().mOnClickListener = l;
    }
ÏÈÅжÏViewÊDz»Êǿɵã»÷µÄ£¬Ö÷ÒªÎÒÃÇÀ´¿´¿´ÏÂÃæÄÇÒ»¾ä£¬getListenerInfo().mOnClickListener = 1;

    ListenerInfo getListenerInfo() {
        if (mListenerInfo != null) {
            return mListenerInfo;
        }
        mListenerInfo = new ListenerInfo();
        return mListenerInfo;
    }
´ÓÕâ¶Î´úÂë¿ÉÒÔ¿´³öÀ´£¬½«ÎÒÃǵÄOnClickListenerʵÀý±£´æµ½ÁËListenerInfor¶ÔÏóÖÐÁË£¬ÄÇôListenerInfor¶ÔÏóÊÇÓÃÀ´¸ÉÂïµÄÄØ£¿ÓÉÓÚÎÒµ±ÏÂûÓÐAndroidϵͳԴÂë¾Í²»ÔÚ¸ú×ÙÏÂÈ¥ÁË£¬¿ÉÒÔ²ÂÏëÕâ¸öÀà³ÖÓÐÎÒÃǵÄOnClickeListener¶ÔÏ󣬵±ÏµÍ³ÏìÓ¦ÆÁÄ»µã»÷ʼþµÄʱºò£¬Í¨¹ýʼþ·Ö·¢£¬¿ÉÒÔµ÷ÓÃonClick·½·¨À´¸æËßËùÓÐʵÏÖÁËOnClickeListener½Ó¿ÚµÄ¶ÔÏó¡£

½ÓÏÂÀ´ÎÒÃÇÀ´Ä£ÄâÒ»ÏÂIOSÖа´Å¥¼àÌýµÄʵÏÖ¡£

\

Button.hÎļþ

<†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cHJlIGNsYXNzPQ=="brush:java;">#import @class Button; //<>´ú±íʵÏÖij¸öЭÒé //ÕâÀïÏ൱ÓÚOnClickListener @protocol ButtonDelegate //½«Button¶ÔÏ󴫵ݸø¼àÌýÆ÷£¬À´ÅжϾßÌåµÄµ÷ÓÃʵÀý - (void) onClick:(Button *)btn; @end @protocol ButtonLongClickDelegate - (void) onLongClick:(Button *)btn; @end @interface Button : NSObject //delegate¾ÍÊÇ°´Å¥µÄ¼àÌýÆ÷ //id´ú±íÈκÎOC¶ÔÏó @property (nonatomic, retain) id delegate; @property (nonatomic, retain) id longClickDeleate; //Ä£Äâϵͳµ÷ÓÃclick·½·¨ - (void)click; //Ä£Äâϵͳµ÷ÓÃlongclick·½·¨ - (void)longClick; @endButton.mÎļþ

#import "Button.h"

@implementation Button

- (void)click {
    //°´Å¥±»µã»÷ÁË£¬¾ÍÓ¦¸Ã֪ͨ¼àÌýÆ÷£¨ÕâÀïÊÇÄ£Ä⣩
    //Èç¹ûonClick·½·¨±»ÊµÏÖ£¬µ÷ÓÃonClick·½·¨
    if([_delegate respondsToSelector:@selector(onClick:)]){
        [_delegate onClick:self];
    }else{
        NSLog(@"onClick¼àÌýÆ÷δʵÏÖ");
    }
}

- (void)longClick {
    //°´Å¥±»³¤°´£¨Ä£Äâϵͳ£©
    if([_delegate respondsToSelector:@selector(onClick:)]){
        [_longClickDeleate onLongClick:self];
    }else{
        NSLog(@"longClick¼àÌýÆ÷δʵÏÖ");
    }
}

- (void)dealloc {
    [_delegate release];
    [_longClickDeleate release];
    [super dealloc];
}

@end
ButtonListener.h

#import 
@protocol ButtonDelegate;

//ʵÏÖ°´Å¥µã»÷ЭÒé
@interface ButtonListener : NSObject 

@end
ButtonListener.m

#import "ButtonListener.h"
#import "Button.h"

@implementation ButtonListener

- (void)onClick:(Button *)btn {
    NSLog(@"°´Å¥±»µã»÷ÁË");
}
@end
ButtonLongClickListener.hÎļþ

#import 

//¶ÔЭÒé½øÐÐÌáÇ°ÉùÃ÷£¬¸ú@classµÄÓÃ;ÊÇÒ»ÑùµÄ
@protocol ButtonLongClickDelegate;

@interface ButtonLongClickListener : NSObject 

@end
ButtonLongClickListener.mÎļþ

#import "ButtonLongClickListener.h"
#import "Button.h"

@implementation ButtonLongClickListener

- (void)onLongClick:(Button *)btn{
    NSLog(@"°´Å¥±»³¤°´ÁË");
}

@end
main.mÎļþ

#import 
#import "Button.h"
#import "ButtonListener.h"
#import "ButtonLongClickListener.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //³õʼ»¯Ò»¸ö°´Å¥
        Button *button = [[[Button alloc] init] autorelease];
        //³õʼ»¯Ò»¸ö°´Å¥µÄ¼àÌýÆ÷
        ButtonListener *listener = [[[ButtonListener alloc] init] autorelease];
        //³õʼ»¯Ò»¸ö°´Å¥³¤°´¼àÌýÆ÷
        ButtonLongClickListener *longClickListener =
            [[[ButtonLongClickListener alloc] init] autorelease];
        //ÉèÖð´Å¥µÄ¼àÌýÆ÷
        button.delegate = listener;
        //ÉèÖó¤°´°´Å¥¼àÌýÆ÷
        button.longClickDeleate = longClickListener;
        //µã»÷°´Å¥
        [button click];
        //³¤°´°´Å¥
        [button longClick];
    }
    return 0;
}
Êä³ö½á¹û£º

2014-11-16 13:52:35.215 ProtocalTest[845:82273] °´Å¥±»µã»÷ÁË

2014-11-16 13:52:35.216 ProtocalTest[845:82273] °´Å¥±»³¤°´ÁË


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved