I just installed xcode 6 to test out how my app works with it.
The facebook login button on the initial page does not appear to be working.
Here is my code:
#property (nonatomic, strong) UIButton *loginButton;
- (void)viewDidLoad
{
[super viewDidLoad];
self.loginButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 70)];
self.loginButton.center = self.view.center;
[self.loginButton setImage:[UIImage imageNamed:#"login_fb.png"] forState:UIControlStateNormal];
[self.view addSubview:self.loginButton];
[self.loginButton addTarget:self action:#selector(loginButtonPressed) forControlEvents:UIControlEventTouchUpInside];
}
-(void)loginButtonPressed
{
// perform login
}
It is important to note that the app starts off on a different view. In that view controller I check to see if a user exists or not, and if not I present the login view. Here is that code:
if ([DCTUserIdentity currentIdentity] == nil) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
DCTInitialLoginVC *vc = [sb instantiateViewControllerWithIdentifier:#"InitialLogin"];
vc.delegate = self;
[self presentViewController:vc animated:NO completion:^{}];
}
I put a breakpoint in the loginButtonPressed function and it never gets hit when I try clicking on the button....any idea whats going on?
It appears that this is not actually a bug, but an incompatibility issue of targeting iOS 7 devices using Xcode 6. ecotax's post describes this in more detail, but here is the response from Apple DTS:
In iOS 7, cells’ content views sized themselves via autoresizing masks. In iOS 8, this was changed, cells stopped using the autoresizing masks and started sizing the content view in layoutSubviews. If a nib is encoded in iOS 8 and then decode it on iOS 7, you’ll have a content view without an autoresizing mask and no other means by which to size itself. So if you ever change the frame of the cell, the content view won’t follow.
Apps being deploying back to iOS 7 will have to work around this by sizing the content view itself, adding autoresizing masks, or adding constraints.
Workaround:
- (void)awakeFromNib
{
// Fix for iOS 7 devices targeted using Xcode 6.
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
Try this code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.loginButton.frame = CGRectMake(0, 0, 200, 70);
self.loginButton.center = self.view.center;
[self.loginButton setImage:[UIImage imageNamed:#"login_fb.png"]
forState:UIControlStateNormal];
[self.view addSubview:self.loginButton];
[self.loginButton addTarget:self action:#selector(loginButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
}
And hold your DCTInitialLoginVC object with strong pointer.
Related
Can someone please explain to me the uibutton target functionality from this example:
I have a ViewController. I add a uiview with two buttons to this viewcontroller. One button is crated in the init and the other one is created by a method 'addSecondButton'. Both buttons have the same action on [self superview] target which is the ViewController. The code:
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
MySpecialView *myspecialview = [[MySpecialView alloc] initWithFrame:self.view.frame];
[self.view addSubview:myspecialview];
[myspecialview addSecondButton];
}
- (void)specialMethod { NSLog(#"right here!"); }
MySpecialView.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 50)];
[button addTarget:[self superview] action:#selector(specialMethod) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blueColor];
[self addSubview:button];
}
return self;
}
- (void)addSecondButton {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 90, self.frame.size.width, 50)];
[button addTarget:[self superview] action:#selector(specialMethod) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor redColor];
[self addSubview:button];
}
So when tapping the blue button which is created in the init, specialMethod does execute. When pressing the red one which is added after the init, the app crashes with the warning - unknow selector for uiview.
What I really don't understand is when I NSLog [self superview] in the init, it is null, because the object is not yet created and returned to the superview, but the action does get executed for some reason.
Thanks for the help!
You have indeed added a nil target for the blue button in initWithFrameMethod. According to the Apple's Event Handling Guide, when you add a nil target, the message (specialMethod in your case) will be passed through the responder chain:
When the user manipulates a control, such as a button or switch, and the target for the action method is nil, the message is sent through a chain of responders starting with the control view.
As your ViewController is a part of the responder chain, it will receive this message and call its specialMethod.
[self superview] is a pointer to ViewController's view, not ViewController itself. The selector you are defining as the action for the button should target the instance of ViewController, not the ViewController's view since you have the method specialMethod defined in ViewController. If you really wanted to use [self superview] as the target for the action, then you'll need to subclass UIView, implement specialMethod and set that new UIView subclass as the view of the view controller (not a subview). This way, [self superview] will refer to a class that actually has the selector specified on the target.
I didn't actually try your code sample, but it's so obviously wrong, if it does work, it's coincidence and shouldn't be relied upon.
i am creating UIButton Programatically.I want a UIViewController to be the target of that button action.How would I do that?.If i click the button navigation will not work.This is my code.please help me anybody.Thanks in advance.
- (void)viewDidLoad
{
weeklyAudio=[UIButton buttonWithType:UIButtonTypeCustom];
[weeklyAudio addTarget:self action:#selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside];
[weeklyAudio setTitle:#"WEEKLY PREDICTIONS" forState:UIControlStateNormal];
weeklyAudio.titleLabel.textAlignment = NSLineBreakByWordWrapping;
weeklyAudio.titleLabel.numberOfLines = 2;
[weeklyAudio.titleLabel setTextAlignment: NSTextAlignmentCenter];
[weeklyAudio setTitleEdgeInsets:UIEdgeInsetsMake(60, 10, 10, 10)];
[weeklyAudio.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
weeklyAudio.frame=CGRectMake(10, 20, 140, 120);
weeklyAudio.layer.cornerRadius=5;
weeklyAudio.backgroundColor=[UIColor colorWithRed:232.0f/255.0f green:52.0f/255.0f blue:27.0f/255.0f alpha:1.0];
[backGroundImage addSubview:weeklyAudio];
weeklyAudioImage=[[UIImageView alloc]initWithFrame:CGRectMake(45, 10, 48, 48)];
weeklyAudioImage.image=[UIImage imageNamed:#"m48*48.png"];
[weeklyAudio addSubview:weeklyAudioImage];
weeklyAudio.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
}
-(void)weeklyPredictions
{
WeeklyViewController *weekly=[[WeeklyViewController alloc]init];
[self.navigationController pushViewController:weekly animated:NO];
}
set a breakpoint in -(void)weeklyPredictions, then debug to see if run in the method when you click the button. If run in it, print po [self navigationController] in console to see if the viewController has a navigation controller . If result is nil, then you can't push another view controller.
Add the following code before addSubview
[weeklyAudio addTarget:self action:#selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside];
And edit your weeklyPredictions method as:
-(void)weeklyPredictions {
WeeklyViewController *weekly=[[WeeklyViewController alloc]initWithNibName:NSStringFromClass([WeeklyViewController class]) bundle:nil];
[self.navigationController pushViewController:weekly animated:NO];
}
Are you using a navigation controller to push and pop viewcontrollers? If not..
use
[self presentViewController:weekly animated:YES completion:nil];
to present your next viewcontroller.
You need to add the following line in your viewDidLoad menthol:
backGroundImage.userInteractionEnabled = YES;
Since by default user interaction is disabled for UIImageView so any subview added to image view will not get touch events.
Hope it will solve your problem, if yes please vote it.
Regards,
Satendra
I’m adding a UISegmentedControl right under the NavigationBar in a UITableViewController. This is the code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationBar = self.navigationController.navigationBar;
UIView *segmentView=[[UIView alloc] initWithFrame:CGRectMake(0, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, 50)];
[segmentView setBackgroundColor:[UIColor whiteColor]];
segmentView.alpha = 0.95;
self.tabSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Favourites", #"All", nil]];
self.tabSegmentedControl.frame = CGRectMake(20, 10, self.navigationBar.frame.size.width - 40, 30);
[self.tabSegmentedControl addTarget:self action:#selector(tabChanged:) forControlEvents:UIControlEventValueChanged];
[segmentView addSubview:self.tabSegmentedControl];
[self.navigationBar addSubview:segmentView];
[self.tabSegmentedControl setSelectedSegmentIndex:1];
}
The view and the SegmentedControl appear on the screen well, but they are not clickable. The selector doesn’t get executed when tapped on the SegmentControl; it doesn’t even switch tabs! In fact, the stuff that is underneath the segmentView (items in the TableView) get clicked when you tap on it. I have tried but failed to understand why this is happening! Any suggestions would be helpful!
You are adding a view below the bounds of its super view. You may see the view however you cannot click it because it is out of bounds. If you set the property of the navigation bar clipsToBounds to YES you should see that the view disappears. What you need to do is add the segment controller to the table view. Here is an example:
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.view addSubview: self.segmentView]; // need to keep a pointer to segmentView
self.tableView.contentInset = UIEdgeInset(self.segmentView.frame.size.height, 0,0,0);
}
-(void) scrollViewDidScroll:(UIScrollView*) scrollView{
CGRect rect = self.segmentView.frame;
rect.origin = self.tableView.contentOffset;
self.segmentView.frame = rect;
}
I am creating an app using iOS 5, and I want to work as follows:
MainViewController w/ NavigationBar, and i set on viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NavViewController *nav = [[NavViewController alloc] init];
[self addChildViewController:nav];
[self.view addSubview:nav.view];
}
Like a "partial view" with some nav controls (my own/custom toolbar), and at NavViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 320, 200);
self.view.frame = viewRect;
self.view.backgroundColor = [UIColor redColor];
}
At this point I have MainViewController with a navigation subview and still have space (in MainViewController view) that another subview can be added using the actions NavViewController.
Is there a serious flaw in this logic? or I can keep developing ?
Thnks a lot.
I am new to IOS programming. I am starting from the basic application created by XCode 4.3 (SingleView application).
Continuing from my above question, I was able to add Views programmaticaly to the main ViewController in the Appdelegate but If I want to do the same after the view has loaded (inside viewDidLoad), I am facing the trouble. I tried using the same logic of adding subviews but I am not sure if the view needs a refresh after adding inside 'viewDidLoad'
My views are very simple, here is the code
-(void)viewDidLoad
{
[super viewDidLoad];
UIBUtton *btn = [UIBUtton] alloc] initWithFrame:CGRect(10,10,50,50)];
[self.view addSubView:btn];
[self.view setNeedsDsiplay];
}
Can someone please help?
You are not instantiating the UIButton correctly. You should use buttonWithType: method.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10.0, 10.0, 50.0, 50.0)];
[[self view] addSubview:btn];
}
You have placed square brackets at wrong places. Probably you are new to Objective-C. You should pay more attention to learning syntax before creating UI based apps.
UIBUtton *btn = [UIBUtton] alloc] initWithFrame:CGRect(10,10,50,50)];
Should be:
UIBUtton *btn = [[UIBUtton alloc] initWithFrame:CGRect(10,10,50,50)];
Also
[self.view setNeedsDsiplay];
is not needed.