Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

ios - How to detect the pressed button and display it on Another View? Objective-C

I am new to iOS app development. I want to create a Calculator App in iOS that has split view. The left side is the "History" Feature in Scroll View and the right side is the calculator itself. Now, regarding the History feature of this app, I am thinking that my program needs to recognize what has been pressed and display it on the Scroll View when the Equal (=) button is pressed. Do you have any idea how will this go on Objective-C? I am using XCode 4.5 and iPhone Simulator 6.0.

Thanks in Advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to communicate/send data between views or view controllers there are several options.

If you try to communicate/send data between views and you have reference to both views you can simply call the methods from your views for example

LeftView.h

@interface LeftView : UIView {
   //instance variables here
}
//properties here
//other methods here
-(NSInteger)giveMeTheValuePlease;
@end

LeftView.m

@implementation LeftView 
//synthesise properties here
//other methods implementation here

-(NSInteger)giveMeTheValuePlease {
   return aValueThatIsInteger; //you can do other computation here
}

RightView.h

  @interface RightView : UIView {
       //instance variables here
    }
    //properties here
    //other methods here
    -(NSInteger) hereIsTheValue:(NSInteger)aValue;
    @end

RightView.m

 @implementation LeftView 
    //synthesise properties here
    //other methods implementation here

    -(void)hereIsTheValue:(NSInteger)aValue {
         //do whatever you want with the value
    }

AViewController.m

@implementation AViewController.m
//these properties must be declared in AViewController.h
@synthesise leftView;
@synthesise rightView;

-(void)someMethod {
   NSInteger aValue = [leftView giveMeTheValuePlease];
   [rightView hereIsTheValue:rightView];
}

You can use the delegate pattern (very very common in iOS), a short and basic example of delegate you can find in one of my SO answer at this link

You can also use blocks to communicate/send data between views/view controllers but this topic I think you will use a little bit later and for you will have to google a little bit in order to get a basic idea of iOS blocks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...