// FilePiece1
// the form of import module No.1
// AController.m
#import Module;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
SubA *info = [[SubA alloc] init];
info.uid = 100;
NSLog(#"%lu", (unsigned long)info.uid);
}
#end
// FilePiece2
// the form of import module No.2
// AController.m
#import Module.subHeadA;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
SubA *info = [[SubA alloc] init];
info.uid = 100;
NSLog(#"%lu", (unsigned long)info.uid);
}
#end
// FilePiece3
// header file of SubA
// Module/subHeadA.h
#interface SubA
#property (nonatomic, assign) UInt32 uid;
#end
// FilePiece4
// header file of SubB, before the change
// Module/subHeadB.h
#interface SubB
#property (nonatomic, assign) UInt32 kk0;
#end
// FilePiece5
// header file of SubB, after the change
// Module/subHeadB.h
#interface SubB
#property (nonatomic, assign) UInt32 kk0;
#property (nonatomic, assign) UInt32 kk1;
#end
If I need to use SubA in ViewController (FilePiece1), and SubB change between two builds (FilePiece4&5), will there be a difference in build performance between two builds? Can Xcode take advantage of the build cache of the first time (FilePiece4) in both import ways (FilePiece1&2)?
Build Process Description:
the first build
FilePiece1/2
FilePiece3
FilePiece4
the second build
FilePiece1/2
FilePiece3
FilePiece5
Related
My code which normally works fine for passing an object to another view controller is not working when the view controller is in a different storyboard.
My code loads the correct view controller embedded in its navigation controller but without any data. (The data object is nil in the destination VC).
Here is the code I'm trying to use;
UIStoryboard *sb2 = [UIStoryboard storyboardWithName:#"secondSB" bundle:nil];
UINavigationController* nav = [sb2 instantiateViewControllerWithIdentifier:#"userNav"];
userDetail *destVC = (userDetail * )nav.topViewController;
NSLog(#"user name%#",user.name);//Logs name showing the user is not empty
destVC.user = user;
[self presentViewController:nav animated:YES completion:nil];
The above loads a VC with no data.
I am able to pass the data object to the VC if I present the VC directly without the navigation controller. But in that case, I lose the navigation functionality which I need.
UIStoryboard *sb2 = [UIStoryboard storyboardWithName:#"secondSB" bundle:nil];
userDetail *destVC = [sb2 instantiateViewControllerWithIdentifier:#"userDetail"];
NSLog(#"user name%#",user.name);//Logs name showing the user is not empty
destVC.user = user;
[self presentViewController:destVC animated:YES completion:nil];
What could be wrong with the above code and what code should I use.
Edit:
I am able to pass a regular object such as a String to the VC embedded in the nav. Or I can pass a custom object to the VC when it is not embedded in the nav. I just can't pass a custom object such as user or I created another NSObject for testing to the VC when embedded in a nav. Perhaps this is some weird glitch when using a different storyboard.
Edit 2
Here is the object code for a light version of user I created in case there was something wrong with the original user object:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
#interface lightUser : NSObject
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * userid;
#property (nonatomic, retain) NSString * descript;
#end
It is a property in the VC:
#import "lightServer.h"
//in interface
#property (weak, nonatomic) lightUser* user;
The following code in ViewDidLoad does not have any effect and the user appears as nil:
self.user.name = #"Hello there";//
po self.user.name in debugger shows nil
po self.user in debuggers shows nil
Not sure without seeing a full example of your code, but I you must be missing something...
Here is a complete example. It should be obvious what gets connected to #IBOutlet and #IBAction (and Storyboard IDs):
UserObject.h
//
// UserObject.h
// Created by Don Mag on 4/1/20.
//
#import <Foundation/Foundation.h>
#interface UserObject : NSObject
#property (strong, nonatomic) NSString *firstName;
#property (strong, nonatomic) NSString *lastName;
#property (assign, readwrite) NSInteger age;
- (NSString *)name;
#end
UserObject.m
//
// UserObject.m
// Created by Don Mag on 4/1/20.
//
#import "UserObject.h"
#implementation UserObject
- (NSString *)name {
return [NSString stringWithFormat:#"%#, %#", _lastName, _firstName];
}
#end
** FirstViewController.h**
//
// FirstViewController.h
// Created by Don Mag on 4/1/20.
//
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#end
** FirstViewController.m**
//
// FirstViewController.m
// Created by Don Mag on 4/1/20.
//
#import "FirstViewController.h"
#import "UserDetailViewController.h"
#import "UserObject.h"
#interface FirstViewController ()
#property (strong, nonatomic) UserObject *aUserObject;
#property (assign, readwrite) NSInteger iAge;
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// initialize age
_iAge = 25;
// initialize a new UserObject
_aUserObject = [UserObject new];
_aUserObject.firstName = #"John";
_aUserObject.lastName = #"Smith";
_aUserObject.age = _iAge;
}
- (IBAction)didTap:(id)sender {
UIStoryboard *sb2 = [UIStoryboard storyboardWithName:#"secondSB" bundle:nil];
UINavigationController* nav = [sb2 instantiateViewControllerWithIdentifier:#"userNav"];
UserDetailViewController *destVC = (UserDetailViewController * )nav.topViewController;
// increment age, so it changes each time we call this method
_iAge++;
_aUserObject.age = _iAge;
destVC.userObj = _aUserObject;
[self presentViewController:nav animated:YES completion:nil];
}
#end
UserDetailViewController.h (VC is in second storyboard)
//
// UserDetailViewController.h
// Created by Don Mag on 3/31/20.
//
#import <UIKit/UIKit.h>
#import "UserObject.h"
#interface UserDetailViewController : UIViewController
#property (strong, nonatomic) UserObject *userObj;
#end
UserDetailViewController.m
//
// UserDetailViewController.m
// Created by Don Mag on 3/31/20.
//
#import "UserDetailViewController.h"
#interface UserDetailViewController ()
#property (strong, nonatomic) IBOutlet UILabel *userLabel;
#end
#implementation UserDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
_userLabel.text = [NSString stringWithFormat:
#"_useObj.firstName: %# \n" \
"_userObj.lastName: %# \n" \
"_userObj.age: %ld \n" \
"_userObj name method: %#",
_userObj.firstName,
_userObj.lastName,
_userObj.age,
[_userObj name]];
}
#end
In case it's not completely clear, here is a working example app: https://github.com/DonMag/DataObjectPassing
I'm pretty new to classes in objective-c. I'm testing out one I made unhiding an image from the class on the click of a button, everything compiles and runs but it's not working out for me, nothing shows up when I click. Am I doing this all wrong? Tried to include everything I think is relevant in the code below, obviously the images for the other parts of the class are there but I didn't include them below seeing as I'm just testing one image from it.
question.m file -
#import "Question.h"
#implementation Question
#synthesize image;
#end
question.h -
#import <UIKit/UIKit.h>
#import "ViewController.h"
#ifndef Question_h
#define Question_h
#endif /* Question_h */
#interface Question : NSObject
{
UIImageView *image;
}
#property(nonatomic, retain) UIImageView *image;
#end
#class Question;
#interface Controller : NSObject
{
Question *firstQuestion;
Question *secondQuestion;
Question *thirdQuestion;
}
#property(nonatomic, retain) Question *firstQuestion;
#property(nonatomic, retain) Question *secondQuestion;
#property(nonatomic, retain) Question *thirdQuestion;
-(void) Question;
ViewController.m -
#import "ViewController.h"
#import "Question.h"
#synthesize question1, q2, q3, firstaid;
- (void)viewDidLoad {
[super viewDidLoad];
firstTierQuestions = [[NSMutableArray alloc] initWithCapacity:100];
}
- (IBAction)Question:(id)sender {
Question *firstQuestion = [[Question alloc] init];
firstQuestion.image = question1;
firstQuestion.a1 = _answer1;
firstQuestion.a2 = _answer2;
[firstTierQuestions addObject:firstQuestion];
- (IBAction)generator:(id)sender {
for (Question *firstQuestion in firstTierQuestions) {
[firstQuestion.image setHidden:NO];
}
}
viewController.h -
#import "question.h"
#interface ViewController : UIViewController
{
IBOutlet UIImageView *question1;
NSMutableArray *firstTierQuestions;
}
#property (strong, nonatomic) IBOutlet UIButton *generator;
#property (strong, nonatomic) IBOutlet UIImageView *question1;
- (IBAction)generator:(id)sender;
#property (nonatomic, strong) NSObject *Question;
#end
I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
I checked several of the suggested links but did not find the answer. I want to pass twoo float values to the initalization of an object breuk. The object has an initalisationmethod initwithTeller: (float) mijnTeller andNoemer: (float) mijnNoemer. The method also returns a float quotient.
First of all I'm getting an error as in initalizing __strong with an expression of incompatible type float. It is given at the initialization of the object breuk in the method berekenQuotient.
I was also wondering if it's necessary to declare the properties mijnTeller and mijnNoemer in breuk.h as they are being passed down via berekenQuotient. tx !
Code:
viewcontroller.h:
#import <UIKit/UIKit.h>
#import "breuk.h"
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *tellerVeld1;
#property (strong, nonatomic) IBOutlet UITextField *noemerVeld1;
#property (strong, nonatomic) IBOutlet UILabel *quotientVeld;
- (IBAction)berekenQuotient:(id)sender;
#end
viewcontroller.M
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize tellerVeld1;
#synthesize noemerVeld1;
#synthesize quotientVeld;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)berekenQuotient:(id)sender {
float teller = [tellerVeld1.text floatValue];
float noemer = [noemerVeld1.text floatValue];
breuk *breuk1 = [[breuk alloc]initWithTeller:teller andNoemer:noemer];
//float quotient = [tellerVeld1.text floatValue]/[noemerVeld1.text floatValue];
//quotientVeld.text = [[NSString alloc]initWithFormat:#"%.2f", quotient];
}
#end
breuk.h
#import <Foundation/Foundation.h>
#interface breuk : NSObject
#property float mijnTeller;
#property float mijnNoemer;
#property float quotient;
- (float) initWithTeller: (float) mijnTeller andNoemer:(float) mijnNoemer;
#end
breuk.m
#import "breuk.h"
#implementation breuk
#synthesize mijnTeller;
#synthesize mijnNoemer;
#synthesize quotient;
- (float)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
return quotient = mijnTeller/mijnNoemer;
}
#end
Your init method doesn't do it's job. Try this:
#interface Breuk : NSObject {
float _mijnTeller;
float _mijnNoemer;
}
#property float mijnTeller;
#property float mijnNoemer;
- (id)initWithTeller:(float)mijnTeller
andNoemer:(float)mijnNoemer;
- (float)quotient;
#end
#implementation Breuk
#synthesize mijnTeller = _mijnTeller;
#synthesize mijnNoemer = _mijnNoemer;
- (id)initWithTeller:(float)mijnTeller
andNoemer:(float)mijnNoemer {
self = [super init];
if (self) {
_mijnTeller = mijnTeller;
_mijnNoemer = mijnNoemer;
}
return self;
}
- (float)quotient {
return mijnTeller / mijnNoemer;
}
#end
All init methods should return an instance of its own class, what you want is just a class methods that does the calculation. Also all classes should start with a capital:
Breuk.h
#import <Foundation/Foundation.h>
#interface Breuk : NSObject
#property float mijnTeller;
#property float mijnNoemer;
#property (readonly) float quotient;
- (instancetype) initWithTeller: (float) mijnTeller andNoemer:(float) mijnNoemer;
#end
Breuk.m
#import "Breuk.h"
#implementation breuk
#synthesize mijnTeller;
#synthesize mijnNoemer;
#synthesize quotient;
- (id)initWithTeller:(float)mijnTeller andNoemer:(float)mijnNoemer{
self = [super init];
if (self) {
self.mijnTeller = mijnTeller;
self.mijnNoemer = mijnNoemer;
}
return self;
}
- (float) quotient {
return mijnTeller/mijnNoemer;
}
#end
Or just with a class methods
Breuk.h
#import <Foundation/Foundation.h>
#interface Breuk : NSObject
+ (float) quotientWithTeller:(float)teller andNoemer:(float)noemer;
#end
Breuk.m
#import "Breuk.h"
#implementation breuk
+ (float) quotientWithTeller:(float)teller andNoemer:(float)noemer;
return teller/noemer;
}
#end
With the class methods you do not need an instance of the Breuk class, just call it on the class:
float quotient = [Breuk quotientWithTeller:teller andNoemer:noemer];
You are getting the error initalizing __strong with an expression of incompatible type float because initWithTeller:andNoemer: returns a float, but you are assigning it to breuk1 which has the type breuk. By default, local variables like breuk1 are strong references, that's where the __strong part of the error comes in.
You don't have to declare the properties mijnTeller and mijnNoemer in breuk.h. You only need to do that if you are going to store those values and then use them again later.
I also see no need for you to try to create a breuk object in the first place, you could just use a class method to perform that calculation.
I've a main class where I want to define two protocols (1 used by a class A, the other by class B) (ios 6.1, xcode 4.6.3 , ARK mode, storyboard project).
According to official syntax, all my code seems to be correct.
But when I try to use the second delegate , nothing work correctly, my 2nd delegate does not respond
**HEADER myProtocols.h**
#import ...
#class myProtocols;
#protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
#end
#protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
#end
#interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
#property (nonatomic, assign) id <myProtocol1> myDelegate1;
#property (nonatomic, assign) id <myProtocol2> myDelegate2;
#end
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
#end
**HEADER classA.h**
#import "myProtocols.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
#interface classB: UIViewController <myProtocol2>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
#end
**MESSAGES classB.m**
#import "classB.h"
#interface classB ()
#end
#implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(#" YES VALUE IS %f and text %# and type %#",myValue2,myText,myType);
}
So, my mistake is to call [_myDelegate2 doOtherStuff..] directly inside a function in myProtocols called only by classA.
Then, if I want to call a function in myProtocols using both delegate I MUST init both these delegates in the class (A or B doesnt matter) I use to call this function:
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
-(void) pleaseDoIt
{
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE NOW WORK VERY WELL**
}
#end
**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
#property(strong, nonatomic) classB *classBVC;
//-(void) doStuff:(float) myValue;
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_classBVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
_myProtoVC.myDelegate2 = _classBVC // THIS IS THE POINT!!!
[_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}