I have subclassed UIScrollView and setting it's super class as delegate as shown in below snippet, even i have set contentSize:, delegate methods are not getting called.
popView = [[PopView alloc]initWithFrame:CGRectMake(35, y, 250, 40)];
popView.answerDelegate = self;
popView.delegate = self;
popView.contentSize = CGSizeMake(750, 40);
popView.scrollEnabled = YES;
[self addSubview:popView];
One more thing, 'PopView' is subclass of UIScrollView.
if you are subclassing UIScrollView then setting its delegate to self will not do anything. a scrollview cannot be its own delegate since it doesnt implement its delegate methods itself. the view that contains the scrollview needs to be the delegate for it and must implement its delegate functions for it to work properly.
also self != super class. super == super class
If you have to adding your Scrollview using Xib then check are set delegate to file owner.and Adding .h file Like this
#interface View : UIView <UIScrollViewDelegate>
if not work yet then trying to clean your build. and then try...
Related
my didAddSubview is not being called.
This is what I am doing.
In my viewController
- (void)viewDidLoad {
[super viewDidLoad];
SomeView *view = [[SomeView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
[self.view addSubview:view];
}
Where my SomeView is a UI view (.h file)
#import <UIKit/UIKit.h>
#interface DyteMeetingView : UIView
#end
in which I have didAddSubview like this
- (void)didAddSubview:(UIView *)subview{
[super didAddSubview:subview];
[self someFunc];
}
I placed debugger in didAddSubview but the debugger never reaches here and hence [self someFunc]; is also not called.
Since didAddSubview is a lifecycle method for UI view, I am also not calling it from anywhere.
Can someone help me in figure out the concept I missed out on/what I could be doing wrong?
PS: I don't have any xib file for UIView.
PS: Intentionally added swift tag as well since there isn't large chunk of code and I am confident that swift developers would be able to understand the question/code and might be able to help me out.
didAddSubview in SomeView will be called when a sub view is added to an instance of SomeView, but that isn't what you are doing here. You are adding a SomeView as a subview of another view.
You want didMoveToSuperview; in this method you can use the superview property of self to identify the view that it moved to.
The didAddSubview will be called in viewController.view, instead of your SomeView, because you using viewController.view.addSubview(subview)
I have an UIView in which an UICollectionview is there. For knowing the scroll distance of UICollectionview I used scrollViewWillBeginDragging: , but it is not getting called.
Sample Code is
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
if(translation.y > 0)
{
//dragging down
_reusableview.backgroundColor = [UIColor blackColor];
}
else
{
// dragging up
_reusableview.backgroundColor = [UIColor clearColor];
}
}
Can anyone help me please?
Hope you have added the UIscrollViewDelegate and set the UIcollectionView delegate to the class.
Then scrollViewWillBeginDragging() function will be called when collectionView is scrolled.
Inside the function you can confirm if the scrollView isKindOfClass UICollectionView.
UICollectionView is a sub class of UIScrollView. Anyone can detect the delegate methods of scroll view by keeping in mind some points.
Set the class as delegate of scroll view
You can do this in .h file and .m file
In .h file
#interface DemoViewController : UIViewController<UIScrollViewDelegate>
{
}
In .m file
#interface SplashViewController ()<UIScrollViewDelegate>
{
}
2. Make the datasource and delegate of collection view that class.
Example:
collectionView.delegate = self;
collectionView.dataSource = self;
Try out above steps.
Hope it will work for you.
I have a UITableViewController and don't want to use a storyboard.
As far as I know, the UITableViewController takes care of initialising the UITableView, connecting it to its dataSource and delegate. This works very well in my case.
I would now like to change the class of the UITableView to a custom class (BVReorderTableView). This would be easily done in IB. However, once I do this programmatically, my UITableView is empty, that is it seems to be disconnected from its source and delegate.
Here is what I do in my init of the UIViewController:
-(id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization
self.tableView = [[BVReorderTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
What am I doing wrong?
You should implement this in viewDidLoad instead of init, because of the ViewController lifecycle. At viewDidLoad he already has all of the objects that are going to get used, which isn't necessarily true during init.
You can check this question for more information on ViewController life cycles.
:)
Please set your UITableView in viewDidLoad method not in initmethod. In init method view will initialize itself before subviews contained within it.
Hope this helps
I am designing a custom view controller in interface builder with a XIB file and I have a custom UIView subclass that I want to add to my view controller. Here is how I've put my custom view into the view controller:
My TonerEffectButtonView class is a subclass of UIView (and has nothing to do with UIButton) and here is my code for it:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)prepareWithSelector:(SEL)selector onTarget:(id)targ withFilter:(GPUImageFilter*)filter{
self.gpuImageView = [[GPUImageView alloc] initWithFrame:self.frame];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:targ action:selector];
[self addGestureRecognizer:gesture];
selectedFilter = filter;
[filter addTarget:self.gpuImageView];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
}
initWithFrame: is not called anyway as I'm creating the view through a XIB, and I don't need any customization in initWithCoder: so I haven't implemented it. I have a GPUImageView (that is a part of a library that I'm using) that I'm creating programatically inside my view, and adding camera input to it (if you ask what happens in the prepare method). Prepare method is called from outside, and I've verified my UIView's frame is correct when it is called. I have a breakpoint on [super drawRect:rect]; and it never gets called, and when I run the program, my custom view is not displayed, nor does receive touch events (but prepare method DOES get called so the instance is created with the correct frame). It's like it has never been put there in interface builder. What could be the cause of this? I've seen many posts about this, and they mostly refer to creating views programatically and indicating problems with 'initWithFrame:'. But I have nothing to do with that method, and I want to use the interface builder/XIB couple, please don't advice me to create the view controller programatically. What could be the cause of it?
Thanks,
Can.
UPDATE: I can verify that -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx is also not called in any way.
Ok, I've found the answer. First, I've tried to add another instance programatically, and I've found out that it has autoresizing mask set to none, whereas my original instance has it set to W+H. This clue made me realize that in fact, I was forgetting to add my GPUImageView instance as a subview into my custom view. I've added it, and it worked.
I have a custom view which contains two UILabel. I want to customize their fonts before so I did that in initWithCoder method.
#implementation HomeTitleView
#synthesize ticketLabel;
#synthesize monthLabel;
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[monthLabel setFont:[UIFactory getFontForKey:#"home_month"]];
[ticketLabel setFont:[UIFactory getFontForKey:#"home_ticket"]];
}
return self;
}
#end
Unluckily, this did not work. Using a debugger, I found that monthLabel and ticketLabel are both nil. Anyone has idea how can I solve this? What callback or method I should implement so that I can access both of my labels?
You can't do that. The views don't exist yet. They are instantiated when the loadView method is called, which happens automatically when the view property is first accessed. If you want to manipulate your views after they have loaded, the correct method to use is viewDidLoad.
Edit: That's assuming you are working with a UIViewController class. If you are working with a UIView class, you can use awakeFromNib or didAddSubview:.
Do you ever assign monthLabel and ticketLabel to UILabel? Something like:
self.monthLabel = [[[UILabel alloc] init] autorelease];
self.ticketLabel = [[[UILabel alloc] init] autorelease];
If so, can you update your post with the code?