A couple of thoughts:
-
You’re written a method,
MysetValue
which is a bit redundant. When you synthesized yourcolumNumber
property, it writes a setter method for you,setColumNumber
, that does this for you. Don’t write your own setter if you don’t need to. -
If you have a property,
columNumber
that you’ve defined in your .h, and synthesized in your .m of yourAppDelegate
(or whatever you called your app delegate class), you can now set this property via:AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; [app setColumNumber:6];
or equivalently,
AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; app.columNumber = 6;
-
Your
DataCell
class is, based upon your clarified answer, not an app delegate class, though. Apparently this is someUIView
based class. You can define custom properties for thisDataCell
class, but you wouldn’t access those properties via[UIApplication sharedApplication]
. You’re apparently saving theseDataCell
objects in an array calledcells
. So if you wanted to set the first cell’scolumNumber
property, you could do something like:DataCell *cell = cells[0]; [cell setColumNumber:6];
-
When I first read the question, I thought you were focusing on communication between classes about properties changing values, for which KVO is useful. But this is a more advanced topic and we shouldn’t worry about that at this point. It’s only going to lead to confusion for a new developer.
As an aside, one of the advantages of using the automatically generated setters methods, is that it automatically conforms to Key-Value Observing (KVO). (If you write your own setters, you can also do the necessary coding to support KVO, but there’s no reason to do so here; use the standard setter.) See the Key-Value Observing Programming Guide. Bottom line, a class can register itself as an observer for changes in a property of another class.
If you’d like to see an example of how to use a property, consider this view subclass:
DetailView.h:
#import <UIKit/UIKit.h>
// public interfaces belong here
@interface DetailView : UIView
@property (nonatomic, readonly) NSInteger columnNumber; // column number set during initialization
@property (nonatomic) NSInteger tapCount; // a count of how many times we tapped on this
- (id)initWithColumnNumber:(NSInteger)column; // interface for creating DetailView
@end
DetailView.m:
#import "DetailView.h"
@implementation DetailView
const CGFloat kWidth = 40.0f;
const CGFloat kMargin = 5.0f;
const CGFloat kHeight = 300.0f;
// create view, set column number, set frame according to column number
- (id)initWithColumnNumber:(NSInteger)column
{
CGRect frame = CGRectMake(kMargin + column * (kWidth + kMargin), kMargin, kWidth, kHeight);
self = [super initWithFrame:frame];
if (self) {
_columnNumber = column;
_tapCount = 0;
self.backgroundColor = [UIColor grayColor];
}
return self;
}
@end
I could then write a view controller that adds these views, but when you tap on one, it will NSLog
the column number for us and tell us how many times we’ve tapped on it:
#import "ViewController.h"
#import "DetailView.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
for (NSInteger column = 0; column < 5; column++)
{
DetailView *detailView = [[DetailView alloc] initWithColumnNumber:column];
[self.view addSubview:detailView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[detailView addGestureRecognizer:tap];
}
}
- (void)handleTap:(UITapGestureRecognizer *)gesture
{
DetailView *detailView = (id)gesture.view;
NSAssert([detailView isKindOfClass:[DetailView class]], @"%s: Not a DetailView: %@", __FUNCTION__, detailView);
detailView.tapCount++;
NSLog(@"%s: column number = %d; tapped %d times", __FUNCTION__, detailView.columnNumber, detailView.tapCount);
}
@end
12
solved How to Set integer from controller to Subview without calling [closed]